Skip to content

Commit 31bfdde

Browse files
authored
Unrolled build for rust-lang#140498
Rollup merge of rust-lang#140498 - compiler-errors:check-fn-tweaks, r=oli-obk Misc tweaks to HIR typeck (mostly w.r.t. checking calls) Just some cleanups. r? oli-obk
2 parents 7188f45 + f986d12 commit 31bfdde

File tree

6 files changed

+91
-139
lines changed

6 files changed

+91
-139
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+38-40
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::adjustment::{
1414
use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt};
1515
use rustc_middle::{bug, span_bug};
1616
use rustc_span::def_id::LocalDefId;
17-
use rustc_span::{Ident, Span, sym};
17+
use rustc_span::{Span, sym};
1818
use rustc_trait_selection::error_reporting::traits::DefIdOrName;
1919
use rustc_trait_selection::infer::InferCtxtExt as _;
2020
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
@@ -87,14 +87,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8787

8888
let output = match result {
8989
None => {
90-
// this will report an error since original_callee_ty is not a fn
91-
self.confirm_builtin_call(
92-
call_expr,
93-
callee_expr,
94-
original_callee_ty,
95-
arg_exprs,
96-
expected,
97-
)
90+
// Check all of the arg expressions, but with no expectations
91+
// since we don't have a signature to compare them to.
92+
for arg in arg_exprs {
93+
self.check_expr(arg);
94+
}
95+
96+
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &callee_expr.kind
97+
&& let [segment] = path.segments
98+
{
99+
self.dcx().try_steal_modify_and_emit_err(
100+
segment.ident.span,
101+
StashKey::CallIntoMethod,
102+
|err| {
103+
// Try suggesting `foo(a)` -> `a.foo()` if possible.
104+
self.suggest_call_as_method(
105+
err, segment, arg_exprs, call_expr, expected,
106+
);
107+
},
108+
);
109+
}
110+
111+
let guar = self.report_invalid_callee(call_expr, callee_expr, expr_ty, arg_exprs);
112+
Ty::new_error(self.tcx, guar)
98113
}
99114

100115
Some(CallStep::Builtin(callee_ty)) => {
@@ -296,9 +311,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
296311
Ty::new_tup_from_iter(self.tcx, arg_exprs.iter().map(|e| self.next_ty_var(e.span)))
297312
});
298313

299-
if let Some(ok) = self.lookup_method_in_trait(
314+
if let Some(ok) = self.lookup_method_for_operator(
300315
self.misc(call_expr.span),
301-
Ident::with_dummy_span(method_name),
316+
method_name,
302317
trait_def_id,
303318
adjusted_ty,
304319
opt_input_type,
@@ -461,32 +476,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
461476
}
462477
(fn_sig, Some(def_id))
463478
}
479+
464480
// FIXME(const_trait_impl): these arms should error because we can't enforce them
465481
ty::FnPtr(sig_tys, hdr) => (sig_tys.with(hdr), None),
466-
_ => {
467-
for arg in arg_exprs {
468-
self.check_expr(arg);
469-
}
470482

471-
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &callee_expr.kind
472-
&& let [segment] = path.segments
473-
{
474-
self.dcx().try_steal_modify_and_emit_err(
475-
segment.ident.span,
476-
StashKey::CallIntoMethod,
477-
|err| {
478-
// Try suggesting `foo(a)` -> `a.foo()` if possible.
479-
self.suggest_call_as_method(
480-
err, segment, arg_exprs, call_expr, expected,
481-
);
482-
},
483-
);
484-
}
485-
486-
let err = self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs);
487-
488-
return Ty::new_error(self.tcx, err);
489-
}
483+
_ => unreachable!(),
490484
};
491485

492486
// Replace any late-bound regions that appear in the function
@@ -908,19 +902,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
908902
call_expr: &'tcx hir::Expr<'tcx>,
909903
arg_exprs: &'tcx [hir::Expr<'tcx>],
910904
expected: Expectation<'tcx>,
911-
method_callee: MethodCallee<'tcx>,
905+
method: MethodCallee<'tcx>,
912906
) -> Ty<'tcx> {
913-
let output_type = self.check_method_argument_types(
907+
self.check_argument_types(
914908
call_expr.span,
915909
call_expr,
916-
Ok(method_callee),
910+
&method.sig.inputs()[1..],
911+
method.sig.output(),
912+
expected,
917913
arg_exprs,
914+
method.sig.c_variadic,
918915
TupleArgumentsFlag::TupleArguments,
919-
expected,
916+
Some(method.def_id),
920917
);
921918

922-
self.write_method_call_and_enforce_effects(call_expr.hir_id, call_expr.span, method_callee);
923-
output_type
919+
self.write_method_call_and_enforce_effects(call_expr.hir_id, call_expr.span, method);
920+
921+
method.sig.output()
924922
}
925923
}
926924

compiler/rustc_hir_typeck/src/expr.rs

+36-20
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use tracing::{debug, instrument, trace};
4040
use {rustc_ast as ast, rustc_hir as hir};
4141

4242
use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
43-
use crate::TupleArgumentsFlag::DontTupleArguments;
4443
use crate::coercion::{CoerceMany, DynamicCoerceMany};
4544
use crate::errors::{
4645
AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
@@ -51,8 +50,8 @@ use crate::errors::{
5150
YieldExprOutsideOfCoroutine,
5251
};
5352
use crate::{
54-
BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, Needs, cast, fatally_break_rust,
55-
report_unexpected_variant_res, type_error_struct,
53+
BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, Needs, TupleArgumentsFlag, cast,
54+
fatally_break_rust, report_unexpected_variant_res, type_error_struct,
5655
};
5756

5857
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -1591,28 +1590,45 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15911590
// no need to check for bot/err -- callee does that
15921591
let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t);
15931592

1594-
let method = match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args)
1595-
{
1593+
match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args) {
15961594
Ok(method) => {
1597-
// We could add a "consider `foo::<params>`" suggestion here, but I wasn't able to
1598-
// trigger this codepath causing `structurally_resolve_type` to emit an error.
15991595
self.write_method_call_and_enforce_effects(expr.hir_id, expr.span, method);
1600-
Ok(method)
1596+
1597+
self.check_argument_types(
1598+
segment.ident.span,
1599+
expr,
1600+
&method.sig.inputs()[1..],
1601+
method.sig.output(),
1602+
expected,
1603+
args,
1604+
method.sig.c_variadic,
1605+
TupleArgumentsFlag::DontTupleArguments,
1606+
Some(method.def_id),
1607+
);
1608+
1609+
method.sig.output()
16011610
}
16021611
Err(error) => {
1603-
Err(self.report_method_error(expr.hir_id, rcvr_t, error, expected, false))
1604-
}
1605-
};
1612+
let guar = self.report_method_error(expr.hir_id, rcvr_t, error, expected, false);
16061613

1607-
// Call the generic checker.
1608-
self.check_method_argument_types(
1609-
segment.ident.span,
1610-
expr,
1611-
method,
1612-
args,
1613-
DontTupleArguments,
1614-
expected,
1615-
)
1614+
let err_inputs = self.err_args(args.len(), guar);
1615+
let err_output = Ty::new_error(self.tcx, guar);
1616+
1617+
self.check_argument_types(
1618+
segment.ident.span,
1619+
expr,
1620+
&err_inputs,
1621+
err_output,
1622+
NoExpectation,
1623+
args,
1624+
false,
1625+
TupleArgumentsFlag::DontTupleArguments,
1626+
None,
1627+
);
1628+
1629+
err_output
1630+
}
1631+
}
16161632
}
16171633

16181634
/// Checks use `x.use`.

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

-56
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use crate::fn_ctxt::arg_matrix::{ArgMatrix, Compatibility, Error, ExpectedIdx, P
3333
use crate::fn_ctxt::infer::FnCall;
3434
use crate::gather_locals::Declaration;
3535
use crate::inline_asm::InlineAsmCtxt;
36-
use crate::method::MethodCallee;
3736
use crate::method::probe::IsSuggestion;
3837
use crate::method::probe::Mode::MethodCall;
3938
use crate::method::probe::ProbeScope::TraitsInScope;
@@ -127,61 +126,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
127126
}
128127
}
129128

130-
pub(in super::super) fn check_method_argument_types(
131-
&self,
132-
sp: Span,
133-
expr: &'tcx hir::Expr<'tcx>,
134-
method: Result<MethodCallee<'tcx>, ErrorGuaranteed>,
135-
args_no_rcvr: &'tcx [hir::Expr<'tcx>],
136-
tuple_arguments: TupleArgumentsFlag,
137-
expected: Expectation<'tcx>,
138-
) -> Ty<'tcx> {
139-
let has_error = match method {
140-
Ok(method) => method.args.error_reported().and(method.sig.error_reported()),
141-
Err(guar) => Err(guar),
142-
};
143-
if let Err(guar) = has_error {
144-
let err_inputs = self.err_args(
145-
method.map_or(args_no_rcvr.len(), |method| method.sig.inputs().len() - 1),
146-
guar,
147-
);
148-
let err_output = Ty::new_error(self.tcx, guar);
149-
150-
let err_inputs = match tuple_arguments {
151-
DontTupleArguments => err_inputs,
152-
TupleArguments => vec![Ty::new_tup(self.tcx, &err_inputs)],
153-
};
154-
155-
self.check_argument_types(
156-
sp,
157-
expr,
158-
&err_inputs,
159-
err_output,
160-
NoExpectation,
161-
args_no_rcvr,
162-
false,
163-
tuple_arguments,
164-
method.ok().map(|method| method.def_id),
165-
);
166-
return err_output;
167-
}
168-
169-
let method = method.unwrap();
170-
self.check_argument_types(
171-
sp,
172-
expr,
173-
&method.sig.inputs()[1..],
174-
method.sig.output(),
175-
expected,
176-
args_no_rcvr,
177-
method.sig.c_variadic,
178-
tuple_arguments,
179-
Some(method.def_id),
180-
);
181-
182-
method.sig.output()
183-
}
184-
185129
/// Generic function that factors out common logic from function calls,
186130
/// method calls and overloaded operators.
187131
pub(in super::super) fn check_argument_types(

compiler/rustc_hir_typeck/src/method/mod.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::ty::{
1919
self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TypeVisitableExt,
2020
};
2121
use rustc_middle::{bug, span_bug};
22-
use rustc_span::{ErrorGuaranteed, Ident, Span};
22+
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
2323
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2424
use rustc_trait_selection::traits::{self, NormalizeExt};
2525
use tracing::{debug, instrument};
@@ -329,10 +329,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
329329
/// an obligation for a particular trait with the given self type and checks
330330
/// whether that trait is implemented.
331331
#[instrument(level = "debug", skip(self))]
332-
pub(super) fn lookup_method_in_trait(
332+
pub(super) fn lookup_method_for_operator(
333333
&self,
334334
cause: ObligationCause<'tcx>,
335-
m_name: Ident,
335+
method_name: Symbol,
336336
trait_def_id: DefId,
337337
self_ty: Ty<'tcx>,
338338
opt_rhs_ty: Option<Ty<'tcx>>,
@@ -374,13 +374,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
374374
// Trait must have a method named `m_name` and it should not have
375375
// type parameters or early-bound regions.
376376
let tcx = self.tcx;
377-
let Some(method_item) = self.associated_value(trait_def_id, m_name) else {
377+
// We use `Ident::with_dummy_span` since no built-in operator methods have
378+
// any macro-specific hygeine, so the span's context doesn't really matter.
379+
let Some(method_item) =
380+
self.associated_value(trait_def_id, Ident::with_dummy_span(method_name))
381+
else {
378382
bug!("expected associated item for operator trait")
379383
};
380384

381385
let def_id = method_item.def_id;
382386
if !method_item.is_fn() {
383-
span_bug!(tcx.def_span(def_id), "expected `{m_name}` to be an associated function");
387+
span_bug!(
388+
tcx.def_span(def_id),
389+
"expected `{method_name}` to be an associated function"
390+
);
384391
}
385392

386393
debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);

compiler/rustc_hir_typeck/src/op.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1212
use rustc_middle::ty::{self, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
1313
use rustc_session::errors::ExprParenthesesNeeded;
1414
use rustc_span::source_map::Spanned;
15-
use rustc_span::{Ident, Span, Symbol, sym};
15+
use rustc_span::{Span, Symbol, sym};
1616
use rustc_trait_selection::infer::InferCtxtExt;
1717
use rustc_trait_selection::traits::{FulfillmentError, Obligation, ObligationCtxt};
1818
use tracing::debug;
@@ -975,7 +975,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
975975
lhs_ty, opname, trait_did
976976
);
977977

978-
let opname = Ident::with_dummy_span(opname);
979978
let (opt_rhs_expr, opt_rhs_ty) = opt_rhs.unzip();
980979
let cause = self.cause(
981980
span,
@@ -990,7 +989,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
990989
);
991990

992991
let method =
993-
self.lookup_method_in_trait(cause.clone(), opname, trait_did, lhs_ty, opt_rhs_ty);
992+
self.lookup_method_for_operator(cause.clone(), opname, trait_did, lhs_ty, opt_rhs_ty);
994993
match method {
995994
Some(ok) => {
996995
let method = self.register_infer_ok_obligations(ok);

compiler/rustc_hir_typeck/src/place_op.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::adjustment::{
88
PointerCoercion,
99
};
1010
use rustc_middle::ty::{self, Ty};
11-
use rustc_span::{Ident, Span, sym};
11+
use rustc_span::{Span, sym};
1212
use tracing::debug;
1313
use {rustc_ast as ast, rustc_hir as hir};
1414

@@ -211,13 +211,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
211211
return None;
212212
};
213213

214-
self.lookup_method_in_trait(
215-
self.misc(span),
216-
Ident::with_dummy_span(imm_op),
217-
imm_tr,
218-
base_ty,
219-
opt_rhs_ty,
220-
)
214+
self.lookup_method_for_operator(self.misc(span), imm_op, imm_tr, base_ty, opt_rhs_ty)
221215
}
222216

223217
fn try_mutable_overloaded_place_op(
@@ -237,13 +231,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
237231
return None;
238232
};
239233

240-
self.lookup_method_in_trait(
241-
self.misc(span),
242-
Ident::with_dummy_span(mut_op),
243-
mut_tr,
244-
base_ty,
245-
opt_rhs_ty,
246-
)
234+
self.lookup_method_for_operator(self.misc(span), mut_op, mut_tr, base_ty, opt_rhs_ty)
247235
}
248236

249237
/// Convert auto-derefs, indices, etc of an expression from `Deref` and `Index`

0 commit comments

Comments
 (0)