Skip to content

Commit ec5e685

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 ec5e685

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

compiler/rustc_errors/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ pub enum StashKey {
576576
UndeterminedMacroResolution,
577577
/// Used by `Parser::maybe_recover_trailing_expr`
578578
ExprInPat,
579+
GenericInFieldExpr,
579580
}
580581

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

compiler/rustc_hir_typeck/src/expr.rs

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

3058-
err.emit()
3058+
self.dcx().try_steal_replace_and_emit_err(expr.span, StashKey::GenericInFieldExpr, err)
30593059
}
30603060

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

compiler/rustc_parse/src/parser/expr.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1369,11 +1369,13 @@ 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+
self.dcx()
1375+
.create_err(errors::FieldExpressionWithGeneric(args.span()))
1376+
.stash(span, StashKey::GenericInFieldExpr);
13741377
}
13751378

1376-
let span = lo.to(self.prev_token.span);
13771379
Ok(self.mk_expr(span, ExprKind::Field(self_arg, seg.ident)))
13781380
}
13791381
}

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
[1, 2].into_iter().collect::<Vec<i32>>;
3+
//~^ ERROR attempted to take value of method
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0615]: attempted to take value of method `collect` on type `std::slice::Iter<'_, {integer}>`
2+
--> $DIR/issue-67680-method-call-with-generics-without-parens.rs:2:24
3+
|
4+
LL | [1, 2].into_iter().collect::<Vec<i32>>;
5+
| ^^^^^^^ method, not a field
6+
|
7+
help: use parentheses to call the method
8+
|
9+
LL | [1, 2].into_iter().collect::<Vec<i32>>();
10+
| ++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0615`.

0 commit comments

Comments
 (0)