Skip to content

Commit 9e92f9c

Browse files
authored
Unrolled build for rust-lang#125906
Rollup merge of rust-lang#125906 - compiler-errors:simplify-method-error-args, r=fmease Remove a bunch of redundant args from `report_method_error` Rebased on top of rust-lang#125397 because I had originally asked there (rust-lang#125397 (comment)) for this change to be made, but I just chose to do it myself. r? fmease
2 parents 5ee2dfd + e9957b9 commit 9e92f9c

File tree

8 files changed

+68
-66
lines changed

8 files changed

+68
-66
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::errors::{
1313
YieldExprOutsideOfCoroutine,
1414
};
1515
use crate::fatally_break_rust;
16-
use crate::method::SelfSource;
1716
use crate::type_error_struct;
1817
use crate::CoroutineTypes;
1918
use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
@@ -223,7 +222,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
223222
let ty = ensure_sufficient_stack(|| match &expr.kind {
224223
hir::ExprKind::Path(
225224
qpath @ (hir::QPath::Resolved(..) | hir::QPath::TypeRelative(..)),
226-
) => self.check_expr_path(qpath, expr, args, call),
225+
) => self.check_expr_path(qpath, expr, Some(args), call),
227226
_ => self.check_expr_kind(expr, expected),
228227
});
229228
let ty = self.resolve_vars_if_possible(ty);
@@ -290,7 +289,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
290289
ExprKind::Path(QPath::LangItem(lang_item, _)) => {
291290
self.check_lang_item_path(lang_item, expr)
292291
}
293-
ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr, &[], None),
292+
ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr, None, None),
294293
ExprKind::InlineAsm(asm) => {
295294
// We defer some asm checks as we may not have resolved the input and output types yet (they may still be infer vars).
296295
self.deferred_asm_checks.borrow_mut().push((asm, expr.hir_id));
@@ -502,12 +501,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
502501
&self,
503502
qpath: &'tcx hir::QPath<'tcx>,
504503
expr: &'tcx hir::Expr<'tcx>,
505-
args: &'tcx [hir::Expr<'tcx>],
504+
args: Option<&'tcx [hir::Expr<'tcx>]>,
506505
call: Option<&'tcx hir::Expr<'tcx>>,
507506
) -> Ty<'tcx> {
508507
let tcx = self.tcx;
509508
let (res, opt_ty, segs) =
510-
self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span, Some(args));
509+
self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
511510
let ty = match res {
512511
Res::Err => {
513512
self.suggest_assoc_method_call(segs);
@@ -564,7 +563,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
564563
// We just want to check sizedness, so instead of introducing
565564
// placeholder lifetimes with probing, we just replace higher lifetimes
566565
// with fresh vars.
567-
let span = args.get(i).map(|a| a.span).unwrap_or(expr.span);
566+
let span = args.and_then(|args| args.get(i)).map_or(expr.span, |arg| arg.span);
568567
let input = self.instantiate_binder_with_fresh_vars(
569568
span,
570569
infer::BoundRegionConversionTime::FnCall,
@@ -1331,9 +1330,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13311330
let rcvr_t = self.check_expr(rcvr);
13321331
// no need to check for bot/err -- callee does that
13331332
let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t);
1334-
let span = segment.ident.span;
13351333

1336-
let method = match self.lookup_method(rcvr_t, segment, span, expr, rcvr, args) {
1334+
let method = match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args)
1335+
{
13371336
Ok(method) => {
13381337
// We could add a "consider `foo::<params>`" suggestion here, but I wasn't able to
13391338
// trigger this codepath causing `structurally_resolve_type` to emit an error.
@@ -1342,18 +1341,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13421341
}
13431342
Err(error) => {
13441343
if segment.ident.name != kw::Empty {
1345-
if let Some(err) = self.report_method_error(
1346-
span,
1347-
Some(rcvr),
1348-
rcvr_t,
1349-
segment.ident,
1350-
expr.hir_id,
1351-
SelfSource::MethodCall(rcvr),
1352-
error,
1353-
Some(args),
1354-
expected,
1355-
false,
1356-
) {
1344+
if let Some(err) =
1345+
self.report_method_error(expr.hir_id, rcvr_t, error, expected, false)
1346+
{
13571347
err.emit();
13581348
}
13591349
}
@@ -1362,7 +1352,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13621352
};
13631353

13641354
// Call the generic checker.
1365-
self.check_method_argument_types(span, expr, method, args, DontTupleArguments, expected)
1355+
self.check_method_argument_types(
1356+
segment.ident.span,
1357+
expr,
1358+
method,
1359+
args,
1360+
DontTupleArguments,
1361+
expected,
1362+
)
13661363
}
13671364

13681365
fn check_expr_cast(

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::callee::{self, DeferredCallResolution};
22
use crate::errors::{self, CtorIsPrivate};
3-
use crate::method::{self, MethodCallee, SelfSource};
3+
use crate::method::{self, MethodCallee};
44
use crate::rvalue_scopes;
55
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy};
66
use rustc_data_structures::fx::FxHashSet;
@@ -735,7 +735,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
735735
qpath: &'tcx QPath<'tcx>,
736736
hir_id: HirId,
737737
span: Span,
738-
args: Option<&'tcx [hir::Expr<'tcx>]>,
739738
) -> (Res, Option<LoweredTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) {
740739
debug!(
741740
"resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}",
@@ -828,14 +827,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
828827

829828
if item_name.name != kw::Empty {
830829
if let Some(e) = self.report_method_error(
831-
span,
832-
None,
833-
ty.normalized,
834-
item_name,
835830
hir_id,
836-
SelfSource::QPath(qself),
831+
ty.normalized,
837832
error,
838-
args,
839833
Expectation::NoExpectation,
840834
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
841835
) {

compiler/rustc_hir_typeck/src/method/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ mod prelude_edition_lints;
77
pub mod probe;
88
mod suggest;
99

10-
pub use self::suggest::SelfSource;
1110
pub use self::MethodError::*;
1211

1312
use crate::FnCtxt;

compiler/rustc_hir_typeck/src/method/suggest.rs

+38-20
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use rustc_data_structures::unord::UnordSet;
1616
use rustc_errors::{
1717
codes::*, pluralize, struct_span_code_err, Applicability, Diag, MultiSpan, StashKey,
1818
};
19-
use rustc_hir as hir;
2019
use rustc_hir::def::DefKind;
2120
use rustc_hir::def_id::DefId;
2221
use rustc_hir::lang_items::LangItem;
2322
use rustc_hir::PathSegment;
23+
use rustc_hir::{self as hir, HirId};
2424
use rustc_hir::{ExprKind, Node, QPath};
2525
use rustc_infer::infer::{self, RegionVariableOrigin};
2626
use rustc_middle::bug;
@@ -187,37 +187,56 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
187187
#[instrument(level = "debug", skip(self))]
188188
pub fn report_method_error(
189189
&self,
190-
span: Span,
191-
rcvr_opt: Option<&'tcx hir::Expr<'tcx>>,
190+
call_id: HirId,
192191
rcvr_ty: Ty<'tcx>,
193-
item_name: Ident,
194-
expr_id: hir::HirId,
195-
source: SelfSource<'tcx>,
196192
error: MethodError<'tcx>,
197-
args: Option<&'tcx [hir::Expr<'tcx>]>,
198193
expected: Expectation<'tcx>,
199194
trait_missing_method: bool,
200195
) -> Option<Diag<'_>> {
196+
let (span, sugg_span, source, item_name, args) = match self.tcx.hir_node(call_id) {
197+
hir::Node::Expr(&hir::Expr {
198+
kind: hir::ExprKind::MethodCall(segment, rcvr, args, _),
199+
span,
200+
..
201+
}) => {
202+
(segment.ident.span, span, SelfSource::MethodCall(rcvr), segment.ident, Some(args))
203+
}
204+
hir::Node::Expr(&hir::Expr {
205+
kind: hir::ExprKind::Path(QPath::TypeRelative(rcvr, segment)),
206+
span,
207+
..
208+
})
209+
| hir::Node::Pat(&hir::Pat {
210+
kind:
211+
hir::PatKind::Path(QPath::TypeRelative(rcvr, segment))
212+
| hir::PatKind::Struct(QPath::TypeRelative(rcvr, segment), ..)
213+
| hir::PatKind::TupleStruct(QPath::TypeRelative(rcvr, segment), ..),
214+
span,
215+
..
216+
}) => {
217+
let args = match self.tcx.parent_hir_node(call_id) {
218+
hir::Node::Expr(&hir::Expr {
219+
kind: hir::ExprKind::Call(callee, args), ..
220+
}) if callee.hir_id == call_id => Some(args),
221+
_ => None,
222+
};
223+
(segment.ident.span, span, SelfSource::QPath(rcvr), segment.ident, args)
224+
}
225+
node => unreachable!("{node:?}"),
226+
};
227+
201228
// Avoid suggestions when we don't know what's going on.
202229
if rcvr_ty.references_error() {
203230
return None;
204231
}
205232

206-
let sugg_span = if let SelfSource::MethodCall(expr) = source {
207-
// Given `foo.bar(baz)`, `expr` is `bar`, but we want to point to the whole thing.
208-
self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id)).span
209-
} else {
210-
span
211-
};
212-
213233
match error {
214234
MethodError::NoMatch(mut no_match_data) => {
215235
return self.report_no_match_method_error(
216236
span,
217-
rcvr_opt,
218237
rcvr_ty,
219238
item_name,
220-
expr_id,
239+
call_id,
221240
source,
222241
args,
223242
sugg_span,
@@ -362,7 +381,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
362381

363382
pub fn suggest_use_shadowed_binding_with_method(
364383
&self,
365-
rcvr_opt: Option<&'tcx hir::Expr<'tcx>>,
384+
self_source: SelfSource<'tcx>,
366385
method_name: Ident,
367386
ty_str_reported: &str,
368387
err: &mut Diag<'_>,
@@ -502,7 +521,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
502521
}
503522
}
504523

505-
if let Some(rcvr) = rcvr_opt
524+
if let SelfSource::MethodCall(rcvr) = self_source
506525
&& let hir::ExprKind::Path(QPath::Resolved(_, path)) = rcvr.kind
507526
&& let hir::def::Res::Local(recv_id) = path.res
508527
&& let Some(segment) = path.segments.first()
@@ -548,7 +567,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
548567
pub fn report_no_match_method_error(
549568
&self,
550569
mut span: Span,
551-
rcvr_opt: Option<&'tcx hir::Expr<'tcx>>,
552570
rcvr_ty: Ty<'tcx>,
553571
item_name: Ident,
554572
expr_id: hir::HirId,
@@ -658,7 +676,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
658676

659677
if is_method {
660678
self.suggest_use_shadowed_binding_with_method(
661-
rcvr_opt,
679+
source,
662680
item_name,
663681
&ty_str_reported,
664682
&mut err,

compiler/rustc_hir_typeck/src/pat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
223223
let PatInfo { binding_mode, max_ref_mutbl, top_info: ti, current_depth, .. } = pat_info;
224224

225225
let path_res = match &pat.kind {
226-
PatKind::Path(qpath) => Some(
227-
self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span, None),
228-
),
226+
PatKind::Path(qpath) => {
227+
Some(self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span))
228+
}
229229
_ => None,
230230
};
231231
let adjust_mode = self.calc_adjust_mode(pat, path_res.map(|(res, ..)| res));
@@ -1184,7 +1184,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11841184

11851185
// Resolve the path and check the definition for errors.
11861186
let (res, opt_ty, segments) =
1187-
self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span, None);
1187+
self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span);
11881188
if res == Res::Err {
11891189
let e = tcx.dcx().span_delayed_bug(pat.span, "`Res::Err` but no error emitted");
11901190
self.set_tainted_by_errors(e);

tests/ui/issues/issue-28344.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ error[E0599]: no function or associated item named `bitor` found for trait objec
5252
LL | let g = BitXor::bitor;
5353
| ^^^^^ function or associated item not found in `dyn BitXor<_>`
5454
|
55-
help: there is a method `bitxor` with a similar name, but with different arguments
55+
help: there is a method `bitxor` with a similar name
5656
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
5757

5858
error: aborting due to 4 previous errors; 2 warnings emitted

tests/ui/resolve/issue-82865.stderr

-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ LL | Box::z
1515
LL | mac!();
1616
| ------ in this macro invocation
1717
|
18-
note: if you're trying to build a new `Box<_, _>` consider using one of the following associated functions:
19-
Box::<T>::new
20-
Box::<T>::new_uninit
21-
Box::<T>::new_zeroed
22-
Box::<T>::try_new
23-
and 18 others
24-
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
2518
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
2619

2720
error: aborting due to 2 previous errors

tests/ui/traits/item-privacy.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,15 @@ LL | S::B;
126126
| ^ associated item not found in `S`
127127
|
128128
= help: items from traits can only be used if the trait is in scope
129+
help: there is a method `b` with a similar name
130+
--> $DIR/item-privacy.rs:11:9
131+
|
132+
LL | fn b(&self) { }
133+
| ^^^^^^^^^^^
129134
help: trait `B` which provides `B` is implemented but not in scope; perhaps you want to import it
130135
|
131136
LL + use assoc_const::B;
132137
|
133-
help: there is a method `b` with a similar name
134-
|
135-
LL | S::b;
136-
| ~
137138

138139
error[E0624]: associated constant `A` is private
139140
--> $DIR/item-privacy.rs:101:14

0 commit comments

Comments
 (0)