Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error message for "expected ~[T] but found a ~-box pattern" #10148

Closed
pnkfelix opened this issue Oct 29, 2013 · 6 comments
Closed

improve error message for "expected ~[T] but found a ~-box pattern" #10148

pnkfelix opened this issue Oct 29, 2013 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@pnkfelix
Copy link
Member

Consider this code:

fn f(a: ~[int]) {
    match a {
        ~[b] => { let b:int = b; println!("a: {:?} b: {:d}", a, b); }
        _ => { fail!(); }
    }
}
fn main() {
    let a : ~[int] = ~[1i];
    f(a);
}

When I attempt to compile it (rustc 0.9-pre (97cd495 2013-10-02 01:16:31 -0700)), I get an error like this:

% rustc /tmp/m.rs
/tmp/m.rs:3:8: 3:12 error: mismatched types: expected `~[int]` but found a ~-box pattern
/tmp/m.rs:3         ~[b] => { let b:int = b; println!("a: {:?} b: {:d}", a, b); }
                    ^~~~
error: aborting due to previous error

I do not find the message above useful. My main reaction is: "The whole pattern ~[b] does look like a so-called ~-box pattern to me, which seems like it should fit the needs of a ~[int]."

After inspecting the source code for _match.rs, I think the issue is that I need to explicitly borrow a and then match against &[b], like so:

fn f(a: ~[int]) {
    match &a {
        &[b] => { let b:int = b; println!("a: {:?} b: {:d}", a, b); }
        _ => { fail!(); }
}

though I do not understand why the above compiles, since I would have thought that borrowing a ~[int] via &a would mean I should use match clauses of the form e.g. &~[b], not &[b].

In any case, we need a better error message here, preferably one that cross-references further discussion elsewhere, such as an appropriate (hypothetical) section of the rust.md manual. Note that the current rust.md documentation on match does not suffice to explain what to do here.

@huonw
Copy link
Member

huonw commented Oct 29, 2013

FWIW, all vector matches are unadorned, e.g. the following all print true in IRC-rusti.

match ~[1i] { [1i] => true, _ => false }
match @[1i] { [1i] => true, _ => false }
match &[1i] { [1i] => true, _ => false }

so matching with a pattern like ~[] is matching on something of type ~(<sigil>[T]).

(I do think that either we should have the matches have sigils (e.g. to match strings), or the error message should be more useful.)

@thestinger
Copy link
Contributor

I would love it if we just dropped ~[T] and called it Vec<T>, but it would lose support for managed pointers without a nice garbage collector with a rooting API.

@chklauser
Copy link

Same with strings:

match os::args()[1] {
    ~"echo"   => expr,
    ...
}

results in "mismatched types: expected ~str but found a ~-box pattern" which I find very confusing. Especially, since removing the box ~ causes "mismatched types: expected ~str but found &'static str (expected ~str but found &'static str)"

Using rust pre-nightly from 92f6b92

Edit (Had copied, the wrong error message before, corrected)

@nrc
Copy link
Member

nrc commented Apr 30, 2014

That second error sounds wrong because the types in the brackets don't match the real types. But I think that is a different issue (not sure if there is an issue filed).

This issue will either go away or change (hopefully be easier to fix) when DST is finished (#12938)

@chklauser
Copy link

Ah yes, you're right. I copied the wrong error message while playing around. rustc complains that ~str and &'static str don't match.

I corrected my earlier comment.

@ghost
Copy link

ghost commented Nov 10, 2014

Fixed in #18171.

@ghost ghost closed this as completed Nov 10, 2014
Jarcho pushed a commit to Jarcho/rust that referenced this issue Feb 26, 2023
Do not panic when analyzing the malformed origin of a format string

Fixes rust-lang#10148. This will trigger only when generating format strings while accepting weird things in a procedural macro and setting the span to something which is not a string.

changelog: none
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

5 participants