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

suppress field expr with generics error message if it's a method #134154

Merged
merged 1 commit into from
Dec 12, 2024
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
4 changes: 4 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ pub enum StashKey {
UndeterminedMacroResolution,
/// Used by `Parser::maybe_recover_trailing_expr`
ExprInPat,
/// If in the parser we detect a field expr with turbofish generic params it's possible that
/// it's a method call without parens. If later on in `hir_typeck` we find out that this is
/// the case we suppress this message and we give a better suggestion.
GenericInFieldExpr,
dev-ardi marked this conversation as resolved.
Show resolved Hide resolved
}

fn default_track_diagnostic<R>(diag: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3055,7 +3055,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.help("methods are immutable and cannot be assigned to");
}

err.emit()
// See `StashKey::GenericInFieldExpr` for more info
self.dcx().try_steal_replace_and_emit_err(field.span, StashKey::GenericInFieldExpr, err)
}

fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,11 +1369,14 @@ impl<'a> Parser<'a> {
))
} else {
// Field access `expr.f`
let span = lo.to(self.prev_token.span);
if let Some(args) = seg.args {
self.dcx().emit_err(errors::FieldExpressionWithGeneric(args.span()));
// See `StashKey::GenericInFieldExpr` for more info on why we stash this.
self.dcx()
.create_err(errors::FieldExpressionWithGeneric(args.span()))
.stash(seg.ident.span, StashKey::GenericInFieldExpr);
}

let span = lo.to(self.prev_token.span);
Ok(self.mk_expr(span, ExprKind::Field(self_arg, seg.ident)))
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/parser/bad-name.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
error: field expressions cannot have generic arguments
--> $DIR/bad-name.rs:2:12
|
LL | let x.y::<isize>.z foo;
| ^^^^^^^

error: expected a pattern, found an expression
--> $DIR/bad-name.rs:2:7
|
Expand All @@ -18,5 +12,11 @@ error: expected one of `(`, `.`, `::`, `:`, `;`, `=`, `?`, `|`, or an operator,
LL | let x.y::<isize>.z foo;
| ^^^ expected one of 9 possible tokens

error: field expressions cannot have generic arguments
--> $DIR/bad-name.rs:2:12
|
LL | let x.y::<isize>.z foo;
| ^^^^^^^

error: aborting due to 3 previous errors

1 change: 0 additions & 1 deletion tests/ui/suggestions/method-missing-parentheses.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
fn main() {
let _ = vec![].into_iter().collect::<usize>;
//~^ ERROR attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
//~| ERROR field expressions cannot have generic arguments
}
8 changes: 1 addition & 7 deletions tests/ui/suggestions/method-missing-parentheses.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
error: field expressions cannot have generic arguments
--> $DIR/method-missing-parentheses.rs:2:41
|
LL | let _ = vec![].into_iter().collect::<usize>;
| ^^^^^^^

error[E0615]: attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
--> $DIR/method-missing-parentheses.rs:2:32
|
Expand All @@ -15,6 +9,6 @@ help: use parentheses to call the method
LL | let _ = vec![].into_iter().collect::<usize>();
| ++

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

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