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

make error emitted on impl &Trait nicer #106712

Merged
merged 2 commits into from
Jan 17, 2023
Merged

Conversation

Ezrashaw
Copy link
Contributor

Fixes #106694

Turned out to be simpler than I thought, also added UI test.

Before: (playground)

error: expected one of `:`, `@`, or `|`, found `)`
 --> src/main.rs:2:22
  |
2 | fn foo(_: impl &Trait) {}
  |                      ^ expected one of `:`, `@`, or `|`
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
  |
2 | fn foo(_: impl Trait: &TypeName) {}
  |                ~~~~~~~~~~~~~~~~
help: if this is a type, explicitly ignore the parameter name
  |
2 | fn foo(_: impl _: &Trait) {}
  |                ++

error: expected one of `!`, `(`, `)`, `,`, `?`, `for`, `~`, lifetime, or path, found `&`
 --> src/main.rs:2:16
  |
2 | fn foo(_: impl &Trait) {}
  |               -^ expected one of 9 possible tokens
  |               |
  |               help: missing `,`

error: expected one of `!`, `(`, `,`, `=`, `>`, `?`, `for`, `~`, lifetime, or path, found `&`
 --> src/main.rs:3:11
  |
3 | fn bar<T: &Trait>(_: T) {}
  |           ^ expected one of 10 possible tokens

After:

error: expected a trait, found type
 --> <anon>:2:16
  |
2 | fn foo(_: impl &Trait) {}
  |                -^^^^^
  |                |
  |                help: consider removing the indirection

error: expected a trait, found type
 --> <anon>:3:11
  |
3 | fn bar<T: &Trait>(_: T) {}
  |           -^^^^^
  |           |
  |           help: consider removing the indirection

@rustbot
Copy link
Collaborator

rustbot commented Jan 11, 2023

r? @cjgillot

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 11, 2023
@Ezrashaw
Copy link
Contributor Author

r? @estebank

@rustbot rustbot assigned estebank and unassigned cjgillot Jan 11, 2023
@Ezrashaw Ezrashaw force-pushed the impl-ref-trait branch 4 times, most recently from e311816 to 068d03d Compare January 11, 2023 10:30
@estebank
Copy link
Contributor

Please address the comments above, but you'll have to rebase on top of a fresh master (we've moved tests from src/test to tests.

@Ezrashaw
Copy link
Contributor Author

Ezrashaw commented Jan 12, 2023

@estebank I assume you just want me to address fmease's comments, you didn't have any?

I think that this is blocked until the tests are moved. I think we're good to go.

@Ezrashaw Ezrashaw force-pushed the impl-ref-trait branch 2 times, most recently from b316295 to b244273 Compare January 12, 2023 01:20
@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Jan 12, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2023

Some changes occurred in src/tools/cargo

cc @ehuss

@estebank
Copy link
Contributor

r=me after addressing the last comment (that I hadn't noticed earlier)

@Ezrashaw
Copy link
Contributor Author

Ezrashaw commented Jan 12, 2023

@estebank Also, the tests are failing, I have to rewrite my impl. It'll be a bit.

@rust-log-analyzer

This comment has been minimized.

@Ezrashaw Ezrashaw requested review from estebank and fmease and removed request for estebank January 14, 2023 08:21
@Ezrashaw Ezrashaw requested review from estebank and fmease and removed request for fmease and estebank January 14, 2023 08:21
@Ezrashaw
Copy link
Contributor Author

Ezrashaw commented Jan 14, 2023

@estebank @fmease I've rewritten this PR, it's a lot better now, we don't have those pesky "added to expected tokens" issues. Another review would be great :).

and I seem have no idea how the "re-request review" button works??

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

One last nitpick before merging

// Just get the indirection part of the type.
let span = ty.span.until(path.span);

err.span_suggestion(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
err.span_suggestion(
err.span_suggestion_verbose(

and then re--bless.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks!

path
} else if !self.token.is_path_start() && self.token.can_begin_type() {
// Instead of finding a path (a trait), we found a type.
let mut err = self.struct_span_err(self.token.span, "expected a trait, found type");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let mut err = self.struct_span_err(self.token.span, "expected a trait, found type");
let mut err = self.struct_span_err(self.token.span, "expected a trait, found type start");

Specify "type start" because we haven't parsed a type yet, and the span points at & only, so it would be a "lie" (and potentially confusing) to talk about a type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've done this slightly differently. Instead we will only emit the nice error is there is a proper type, "found type start" seems a bit confusing to me.

@Ezrashaw
Copy link
Contributor Author

@estebank Can we merge this now? Also could you r+ #106591? I don't have perms. Thanks!

@@ -938,6 +940,35 @@ impl<'a> Parser<'a> {
&& self.look_ahead(1, |tok| tok.kind == TokenKind::OpenDelim(Delimiter::Parenthesis))
&& let Some(path) = self.recover_path_from_fn()
{
path
} else if !self.token.is_path_start() && self.token.can_begin_type() && let Ok(ty) = self.parse_ty_no_plus() {
Copy link
Contributor

Choose a reason for hiding this comment

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

The issue I'm seeing is that let Ok(ty) = self.parse_ty_no_plus() can fail parsing the type and will be returning an Err(Diagnostic), which gets discarded. If this happens, an ICE will occur. We need to either bubble it up (wich self.parse_ty_no_plus()?, which is fine if slightly misleading) or err.delay_as_bug() it (which isn't great without another mechanism to complain about the parse failure).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, what about matching on error and cancelling the diagnostic. I didn't properly realize that it returned a diagnostic, not just an error to be later turned into a diagnostic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With my current code the following ICEs:

fn foo<T: &match>() {}

if we ? the parse call then we get:

error: expected type, found keyword `match`
 --> <anon>:1:12
  |
1 | fn foo<T: &match>() {}
  |            ^^^^^ expected type

I'm happy with this, I don't think it's confusing. We could however add a note, something along the lines of: this error was encountered when trying to recover from a "expected a trait, found type" error before bubbling it up.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can do map_err(|mut err| { err.note(..); err })?, but I think even without the extra context this output would be fine.

// to recover from errors, not make more).
let path = if self.may_recover()
&& matches!(ty.kind, TyKind::Ptr(..) | TyKind::Ref(..))
&& let TyKind::Path(_, path) = &ty.peel_refs().kind {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
&& let TyKind::Path(_, path) = &ty.peel_refs().kind {
&& let TyKind::Path(_, path) = &ty.peel_refs().kind
{

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jan 17, 2023

📌 Commit fcd5ed2 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 17, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#106591 (suggestion for attempted integer identifier in patterns)
 - rust-lang#106712 (make error emitted on `impl &Trait` nicer)
 - rust-lang#106829 (Unify `Opaque`/`Projection` handling in region outlives code)
 - rust-lang#106869 (rustdoc: remove redundant item kind class from `.item-decl > pre`)
 - rust-lang#106949 (ConstBlocks are poly if their substs are poly)
 - rust-lang#106953 (Document `EarlyBinder::subst_identity` and `skip_binder`)
 - rust-lang#106958 (Don't add A-bootstrap to PRs modifying Cargo.lock)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9cda9e0 into rust-lang:master Jan 17, 2023
@rustbot rustbot added this to the 1.68.0 milestone Jan 17, 2023
@Ezrashaw Ezrashaw deleted the impl-ref-trait branch January 17, 2023 08:27
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…macro-is-ok, r=estebank

Do not eagerly recover for bad `impl Trait` types in macros

Fixes rust-lang#107796

cc rust-lang#106712, `@estebank` and `@Ezrashaw` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries.

This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…macro-is-ok, r=estebank

Do not eagerly recover for bad `impl Trait` types in macros

Fixes rust-lang#107796

cc rust-lang#106712, ``@estebank`` and ``@Ezrashaw`` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries.

This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…macro-is-ok, r=estebank

Do not eagerly recover for bad `impl Trait` types in macros

Fixes rust-lang#107796

cc rust-lang#106712, ```@estebank``` and ```@Ezrashaw``` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries.

This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recover from impl &Trait and T: &Trait
7 participants