Skip to content

Commit 014363e

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 014363e

File tree

6 files changed

+18
-17
lines changed

6 files changed

+18
-17
lines changed

Diff for: 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 {

Diff for: 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) {

Diff for: 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
}

Diff for: 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

Diff for: tests/ui/suggestions/method-missing-parentheses.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
fn main() {
22
let _ = vec![].into_iter().collect::<usize>;
33
//~^ ERROR attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
4-
//~| ERROR field expressions cannot have generic arguments
54
}
+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
error: field expressions cannot have generic arguments
2-
--> $DIR/method-missing-parentheses.rs:2:41
3-
|
4-
LL | let _ = vec![].into_iter().collect::<usize>;
5-
| ^^^^^^^
6-
71
error[E0615]: attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
82
--> $DIR/method-missing-parentheses.rs:2:32
93
|
@@ -15,6 +9,6 @@ help: use parentheses to call the method
159
LL | let _ = vec![].into_iter().collect::<usize>();
1610
| ++
1711

18-
error: aborting due to 2 previous errors
12+
error: aborting due to 1 previous error
1913

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

0 commit comments

Comments
 (0)