Skip to content

Commit 5aebe6f

Browse files
author
Orion Gonzalez
committed
Don't emit "field expressions may not have generic arguments" if it's a method call without ()
1 parent 33c245b commit 5aebe6f

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

compiler/rustc_errors/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ pub enum StashKey {
576576
UndeterminedMacroResolution,
577577
/// Used by `Parser::maybe_recover_trailing_expr`
578578
ExprInPat,
579+
/// If in the parser we detect a field expr with turbofish generic params it's possible that
580+
/// it's a method call without parens. If later on in `hir_typeck` we find out that this is
581+
/// the case we suppress this message and we give a better suggestion.
582+
GenericInFieldExpr,
579583
}
580584

581585
fn default_track_diagnostic<R>(diag: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {

compiler/rustc_hir_typeck/src/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3055,7 +3055,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30553055
err.help("methods are immutable and cannot be assigned to");
30563056
}
30573057

3058-
err.emit()
3058+
// See `StashKey::GenericInFieldExpr` for more info
3059+
self.dcx().try_steal_replace_and_emit_err(field.span, StashKey::GenericInFieldExpr, err)
30593060
}
30603061

30613062
fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {

compiler/rustc_parse/src/parser/expr.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1369,11 +1369,14 @@ impl<'a> Parser<'a> {
13691369
))
13701370
} else {
13711371
// Field access `expr.f`
1372+
let span = lo.to(self.prev_token.span);
13721373
if let Some(args) = seg.args {
1373-
self.dcx().emit_err(errors::FieldExpressionWithGeneric(args.span()));
1374+
// See `StashKey::GenericInFieldExpr` for more info on why we stash this.
1375+
self.dcx()
1376+
.create_err(errors::FieldExpressionWithGeneric(args.span()))
1377+
.stash(seg.ident.span, StashKey::GenericInFieldExpr);
13741378
}
13751379

1376-
let span = lo.to(self.prev_token.span);
13771380
Ok(self.mk_expr(span, ExprKind::Field(self_arg, seg.ident)))
13781381
}
13791382
}

tests/ui/parser/bad-name.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
error: field expressions cannot have generic arguments
2-
--> $DIR/bad-name.rs:2:12
3-
|
4-
LL | let x.y::<isize>.z foo;
5-
| ^^^^^^^
6-
71
error: expected a pattern, found an expression
82
--> $DIR/bad-name.rs:2:7
93
|
@@ -18,5 +12,11 @@ error: expected one of `(`, `.`, `::`, `:`, `;`, `=`, `?`, `|`, or an operator,
1812
LL | let x.y::<isize>.z foo;
1913
| ^^^ expected one of 9 possible tokens
2014

15+
error: field expressions cannot have generic arguments
16+
--> $DIR/bad-name.rs:2:12
17+
|
18+
LL | let x.y::<isize>.z foo;
19+
| ^^^^^^^
20+
2121
error: aborting due to 3 previous errors
2222

0 commit comments

Comments
 (0)