Skip to content

Suggest parentheses when a struct literal needs them #51360

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

Merged
merged 1 commit into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2934,8 +2934,38 @@ impl<'a> Resolver<'a> {
here due to private fields"));
}
} else {
err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?",
path_str));
// HACK(estebank): find a better way to figure out that this was a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is .. certainly a hack. I don't know what would be the best way to handle it, though; we have an id, so if we had the AST on hand we could find the context for this path by -- at worst -- walking through it. I'm not sure if there is a "map" for the AST that lets you find out (e.g.) the parent node?

That said, this function is invoked (iirc) via a visit walk, so perhaps we could add some kind of "context" parameter and thread the information down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the AST is already incorrect. I would like to modify the parser to accept struct literals in these positions to minimize errors being shown and include the suggestion then, but wanted to have a quick solution until then as this is a problem I've seen crop up multiple times on the issue tracker and in the forums.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, my point regarding hack was not the approach of making a suggestion, which makes sense to me, but rather using the spans etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, re-reading this diff, I see I was a bit confused about what was going on. This now seems more reasonable than I thought at first. =)

// parser issue where a struct literal is being used on an expression
// where a brace being opened means a block is being started. Look
// ahead for the next text to see if `span` is followed by a `{`.
let cm = this.session.codemap();
let mut sp = span;
loop {
sp = cm.next_point(sp);
match cm.span_to_snippet(sp) {
Ok(ref snippet) => {
if snippet.chars().any(|c| { !c.is_whitespace() }) {
break;
}
}
_ => break,
}
}
let followed_by_brace = match cm.span_to_snippet(sp) {
Ok(ref snippet) if snippet == "{" => true,
_ => false,
};
if let (PathSource::Expr(None), true) = (source, followed_by_brace) {
err.span_label(
span,
format!("did you mean `({} {{ /* fields */ }})`?", path_str),
);
} else {
err.span_label(
span,
format!("did you mean `{} {{ /* fields */ }}`?", path_str),
);
}
}
return (err, candidates);
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/error-codes/E0423.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,22 @@ fn main () {

let f = Foo(); //~ ERROR E0423
}

fn bar() {
struct S { x: i32, y: i32 }
#[derive(PartialEq)]
struct T {}

if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
//~^ ERROR E0423
//~| expected type, found `1`
if T {} == T {} { println!("Ok"); }
//~^ ERROR E0423
//~| ERROR expected expression, found `==`
}

fn foo() {
for _ in std::ops::Range { start: 0, end: 10 } {}
//~^ ERROR E0423
//~| ERROR expected type, found `0`
}
43 changes: 41 additions & 2 deletions src/test/ui/error-codes/E0423.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
error: expected type, found `1`
--> $DIR/E0423.rs:22:39
|
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
| ^ expecting a type here because of type ascription

error: expected expression, found `==`
--> $DIR/E0423.rs:25:13
|
LL | if T {} == T {} { println!("Ok"); }
| ^^ expected expression

error: expected type, found `0`
--> $DIR/E0423.rs:31:39
|
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
| ^ expecting a type here because of type ascription

error[E0423]: expected function, found struct `Foo`
--> $DIR/E0423.rs:14:13
|
LL | let f = Foo(); //~ ERROR E0423
| ^^^ did you mean `Foo { /* fields */ }`?
| ^^^
| |
| did you mean `foo`?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this new test confusing -- it seems to combine a bunch of stuff that's hard to sort out. It took me a while to see that, by adding foo, you triggered a new suggestion here, for example. Perhaps it'd be better to make a new test file (or even multiple new files)? (Or did you intentionally want to demonstrate this interaction / cover all these cases simultaneously?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I originally introduced that suggestion, it was by accident due to reusing the similar identifier. Once I saw it, I thought it was a good idea to keep it as I'm not sure we're exercising the multiple suggestion scenario anywhere else. If you feel that this warrants breaking up into smaller tests, I can do that.

| did you mean `Foo { /* fields */ }`?

error[E0423]: expected value, found struct `S`
--> $DIR/E0423.rs:22:32
|
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
| ^ did you mean `(S { /* fields */ })`?

error[E0423]: expected value, found struct `T`
--> $DIR/E0423.rs:25:8
|
LL | if T {} == T {} { println!("Ok"); }
| ^ did you mean `(T { /* fields */ })`?

error[E0423]: expected value, found struct `std::ops::Range`
--> $DIR/E0423.rs:31:14
|
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
| ^^^^^^^^^^^^^^^ did you mean `(std::ops::Range { /* fields */ })`?

error: aborting due to previous error
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0423`.