Skip to content

Commit

Permalink
Deduplicate check_expr in builtin calls with error
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Dec 5, 2022
1 parent 53e4b9d commit 87efc9a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
24 changes: 10 additions & 14 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
ty::FnPtr(sig) => (sig, None),
_ => {
let mut skip_first_expr = false;
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &callee_expr.kind
&& let [segment] = path.segments
&& let Some(mut diag) = self
Expand All @@ -421,24 +422,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return ty;
} else {
diag.emit();
skip_first_expr = true;
}
}

self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs);

// This is the "default" function signature, used in case of error.
// In that case, we check each argument against "error" in order to
// set up all the node type bindings.
(
ty::Binder::dummy(self.tcx.mk_fn_sig(
self.err_args(arg_exprs.len()).into_iter(),
self.tcx.ty_error(),
false,
hir::Unsafety::Normal,
abi::Abi::Rust,
)),
None,
)
for arg in arg_exprs.iter().skip(skip_first_expr as usize) {
self.check_expr(arg);
}

return self.tcx.ty_error();
}
};

Expand Down Expand Up @@ -498,7 +492,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected: Expectation<'tcx>,
) -> Option<Ty<'tcx>> {
if let [callee_expr, rest @ ..] = arg_exprs {
let callee_ty = self.check_expr(callee_expr);
// This may happen recursively -- if so, avoid repeatedly checking the expr.
let callee_ty = self.typeck_results.borrow().expr_ty_adjusted_opt(callee_expr);
let callee_ty = callee_ty.unwrap_or_else(|| self.check_expr(callee_expr));
// First, do a probe with `IsSuggestion(true)` to avoid emitting
// any strange errors. If it's successful, then we'll do a true
// method lookup.
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/suggestions/fn-to-method-deeply-nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() -> Result<(), ()> {
a(b(c(d(e(
//~^ ERROR cannot find function `a` in this scope
//~| ERROR cannot find function `b` in this scope
//~| ERROR cannot find function `c` in this scope
//~| ERROR cannot find function `d` in this scope
//~| ERROR cannot find function `e` in this scope
z????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????
//~^^^ ERROR cannot find value `z` in this scope
)))))
}
39 changes: 39 additions & 0 deletions src/test/ui/suggestions/fn-to-method-deeply-nested.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[E0425]: cannot find value `z` in this scope
--> $DIR/fn-to-method-deeply-nested.rs:8:9
|
LL | z????????????????????????????????????????????????????????????????????????????????????????
| ^ not found in this scope

error[E0425]: cannot find function `e` in this scope
--> $DIR/fn-to-method-deeply-nested.rs:2:13
|
LL | a(b(c(d(e(
| ^ not found in this scope

error[E0425]: cannot find function `d` in this scope
--> $DIR/fn-to-method-deeply-nested.rs:2:11
|
LL | a(b(c(d(e(
| ^ not found in this scope

error[E0425]: cannot find function `c` in this scope
--> $DIR/fn-to-method-deeply-nested.rs:2:9
|
LL | a(b(c(d(e(
| ^ not found in this scope

error[E0425]: cannot find function `b` in this scope
--> $DIR/fn-to-method-deeply-nested.rs:2:7
|
LL | a(b(c(d(e(
| ^ not found in this scope

error[E0425]: cannot find function `a` in this scope
--> $DIR/fn-to-method-deeply-nested.rs:2:5
|
LL | a(b(c(d(e(
| ^ not found in this scope

error: aborting due to 6 previous errors

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

0 comments on commit 87efc9a

Please sign in to comment.