Skip to content

Commit

Permalink
Don't emit "field expressions may not have generic arguments" if it's…
Browse files Browse the repository at this point in the history
… a method call without ()
  • Loading branch information
Orion Gonzalez committed Dec 11, 2024
1 parent 33c245b commit ec5e685
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ pub enum StashKey {
UndeterminedMacroResolution,
/// Used by `Parser::maybe_recover_trailing_expr`
ExprInPat,
GenericInFieldExpr,
}

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

err.emit()
self.dcx().try_steal_replace_and_emit_err(expr.span, StashKey::GenericInFieldExpr, err)
}

fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {
Expand Down
6 changes: 4 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,13 @@ 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()));
self.dcx()
.create_err(errors::FieldExpressionWithGeneric(args.span()))
.stash(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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
[1, 2].into_iter().collect::<Vec<i32>>;
//~^ ERROR attempted to take value of method
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0615]: attempted to take value of method `collect` on type `std::slice::Iter<'_, {integer}>`
--> $DIR/issue-67680-method-call-with-generics-without-parens.rs:2:24
|
LL | [1, 2].into_iter().collect::<Vec<i32>>;
| ^^^^^^^ method, not a field
|
help: use parentheses to call the method
|
LL | [1, 2].into_iter().collect::<Vec<i32>>();
| ++

error: aborting due to 1 previous error

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

0 comments on commit ec5e685

Please sign in to comment.