Skip to content

Commit e5b278b

Browse files
Deduplicate check_expr in builtin calls with error
1 parent e5e4eef commit e5b278b

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
399399
}
400400
ty::FnPtr(sig) => (sig, None),
401401
_ => {
402+
let mut skip_first_expr = false;
402403
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &callee_expr.kind
403404
&& let [segment] = path.segments
404405
&& let Some(mut diag) = self
@@ -421,11 +422,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
421422
return ty;
422423
} else {
423424
diag.emit();
425+
skip_first_expr = true;
424426
}
425427
}
426428

427429
let err = self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs);
428430

431+
for arg in arg_exprs.iter().skip(skip_first_expr as usize) {
432+
self.check_expr(arg);
433+
}
434+
429435
return self.tcx.ty_error_with_guaranteed(err);
430436
}
431437
};
@@ -486,7 +492,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
486492
expected: Expectation<'tcx>,
487493
) -> Option<Ty<'tcx>> {
488494
if let [callee_expr, rest @ ..] = arg_exprs {
489-
let callee_ty = self.check_expr(callee_expr);
495+
// This may happen recursively -- if so, avoid repeatedly checking the expr.
496+
let callee_ty = self.typeck_results.borrow().expr_ty_adjusted_opt(callee_expr);
497+
let callee_ty = callee_ty.unwrap_or_else(|| self.check_expr(callee_expr));
490498
// First, do a probe with `IsSuggestion(true)` to avoid emitting
491499
// any strange errors. If it's successful, then we'll do a true
492500
// method lookup.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() -> Result<(), ()> {
2+
a(b(c(d(e(
3+
//~^ ERROR cannot find function `a` in this scope
4+
//~| ERROR cannot find function `b` in this scope
5+
//~| ERROR cannot find function `c` in this scope
6+
//~| ERROR cannot find function `d` in this scope
7+
//~| ERROR cannot find function `e` in this scope
8+
z????????????????????????????????????????????????????????????????????????????????????????
9+
?????????????????????????????????????????????????????????????????????????????????????????
10+
??????????????????????????????????????????????????????????????????
11+
//~^^^ ERROR cannot find value `z` in this scope
12+
)))))
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0425]: cannot find value `z` in this scope
2+
--> $DIR/fn-to-method-deeply-nested.rs:8:9
3+
|
4+
LL | z????????????????????????????????????????????????????????????????????????????????????????
5+
| ^ not found in this scope
6+
7+
error[E0425]: cannot find function `e` in this scope
8+
--> $DIR/fn-to-method-deeply-nested.rs:2:13
9+
|
10+
LL | a(b(c(d(e(
11+
| ^ not found in this scope
12+
13+
error[E0425]: cannot find function `d` in this scope
14+
--> $DIR/fn-to-method-deeply-nested.rs:2:11
15+
|
16+
LL | a(b(c(d(e(
17+
| ^ not found in this scope
18+
19+
error[E0425]: cannot find function `c` in this scope
20+
--> $DIR/fn-to-method-deeply-nested.rs:2:9
21+
|
22+
LL | a(b(c(d(e(
23+
| ^ not found in this scope
24+
25+
error[E0425]: cannot find function `b` in this scope
26+
--> $DIR/fn-to-method-deeply-nested.rs:2:7
27+
|
28+
LL | a(b(c(d(e(
29+
| ^ not found in this scope
30+
31+
error[E0425]: cannot find function `a` in this scope
32+
--> $DIR/fn-to-method-deeply-nested.rs:2:5
33+
|
34+
LL | a(b(c(d(e(
35+
| ^ not found in this scope
36+
37+
error: aborting due to 6 previous errors
38+
39+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)