Skip to content

Commit ac385a5

Browse files
committed
Auto merge of rust-lang#125120 - compiler-errors:rollup-mnjybwv, r=compiler-errors
Rollup of 7 pull requests Successful merges: - rust-lang#119838 (style-guide: When breaking binops handle multi-line first operand better) - rust-lang#124844 (Use a proper probe for shadowing impl) - rust-lang#125047 (Migrate `run-make/issue-14500` to new `rmake.rs` format) - rust-lang#125080 (only find segs chain for missing methods when no available candidates) - rust-lang#125088 (Uplift `AliasTy` and `AliasTerm`) - rust-lang#125100 (Don't do post-method-probe error reporting steps if we're in a suggestion) - rust-lang#125118 (Use new utility functions/methods in run-make tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents bdfd941 + 31016d5 commit ac385a5

File tree

42 files changed

+1042
-599
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1042
-599
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::ExistentialTrait
106106
}
107107
}
108108

109+
impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::UnevaluatedConst<I> {
110+
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
111+
format!("{self:?}").into_diag_arg()
112+
}
113+
}
114+
109115
into_diag_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
110116

111117
impl IntoDiagArg for bool {

compiler/rustc_hir_typeck/src/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -938,14 +938,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
938938
);
939939
}
940940

941-
pub fn get_conversion_methods(
941+
pub fn get_conversion_methods_for_diagnostic(
942942
&self,
943943
span: Span,
944944
expected: Ty<'tcx>,
945945
checked_ty: Ty<'tcx>,
946946
hir_id: hir::HirId,
947947
) -> Vec<AssocItem> {
948-
let methods = self.probe_for_return_type(
948+
let methods = self.probe_for_return_type_for_diagnostic(
949949
span,
950950
probe::Mode::MethodCall,
951951
expected,

compiler/rustc_hir_typeck/src/expr.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24142414

24152415
let guar = if field.name == kw::Empty {
24162416
self.dcx().span_delayed_bug(field.span, "field name with no name")
2417-
} else if self.method_exists(field, base_ty, expr.hir_id, expected.only_has_type(self)) {
2417+
} else if self.method_exists_for_diagnostic(
2418+
field,
2419+
base_ty,
2420+
expr.hir_id,
2421+
expected.only_has_type(self),
2422+
) {
24182423
self.ban_take_value_of_method(expr, base_ty, field)
24192424
} else if !base_ty.is_primitive_ty() {
24202425
self.ban_nonexisting_field(field, base, expr, base_ty)
@@ -2600,7 +2605,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26002605
let mut err = self.private_field_err(field, base_did);
26012606

26022607
// Also check if an accessible method exists, which is often what is meant.
2603-
if self.method_exists(field, expr_t, expr.hir_id, return_ty)
2608+
if self.method_exists_for_diagnostic(field, expr_t, expr.hir_id, return_ty)
26042609
&& !self.expr_in_place(expr.hir_id)
26052610
{
26062611
self.suggest_method_call(

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
290290
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
291291
) -> bool {
292292
let expr = expr.peel_blocks();
293-
let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id);
293+
let methods =
294+
self.get_conversion_methods_for_diagnostic(expr.span, expected, found, expr.hir_id);
294295

295296
if let Some((suggestion, msg, applicability, verbose, annotation)) =
296297
self.suggest_deref_or_ref(expr, found, expected)

compiler/rustc_hir_typeck/src/method/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub enum CandidateSource {
9191
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9292
/// Determines whether the type `self_ty` supports a visible method named `method_name` or not.
9393
#[instrument(level = "debug", skip(self))]
94-
pub fn method_exists(
94+
pub fn method_exists_for_diagnostic(
9595
&self,
9696
method_name: Ident,
9797
self_ty: Ty<'tcx>,
@@ -102,7 +102,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102102
probe::Mode::MethodCall,
103103
method_name,
104104
return_type,
105-
IsSuggestion(false),
105+
IsSuggestion(true),
106106
self_ty,
107107
call_expr_id,
108108
ProbeScope::TraitsInScope,

compiler/rustc_hir_typeck/src/method/probe.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
9090
>,
9191

9292
scope_expr_id: HirId,
93+
94+
/// Is this probe being done for a diagnostic? This will skip some error reporting
95+
/// machinery, since we don't particularly care about, for example, similarly named
96+
/// candidates if we're *reporting* similarly named candidates.
97+
is_suggestion: IsSuggestion,
9398
}
9499

95100
impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
@@ -220,7 +225,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
220225
/// would use to decide if a method is a plausible fit for
221226
/// ambiguity purposes).
222227
#[instrument(level = "debug", skip(self, candidate_filter))]
223-
pub fn probe_for_return_type(
228+
pub fn probe_for_return_type_for_diagnostic(
224229
&self,
225230
span: Span,
226231
mode: Mode,
@@ -459,6 +464,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
459464
&orig_values,
460465
steps.steps,
461466
scope_expr_id,
467+
is_suggestion,
462468
);
463469

464470
probe_cx.assemble_inherent_candidates();
@@ -553,6 +559,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
553559
orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
554560
steps: &'tcx [CandidateStep<'tcx>],
555561
scope_expr_id: HirId,
562+
is_suggestion: IsSuggestion,
556563
) -> ProbeContext<'a, 'tcx> {
557564
ProbeContext {
558565
fcx,
@@ -570,6 +577,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
570577
static_candidates: RefCell::new(Vec::new()),
571578
unsatisfied_predicates: RefCell::new(Vec::new()),
572579
scope_expr_id,
580+
is_suggestion,
573581
}
574582
}
575583

@@ -944,6 +952,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
944952
return r;
945953
}
946954

955+
// If it's a `lookup_probe_for_diagnostic`, then quit early. No need to
956+
// probe for other candidates.
957+
if self.is_suggestion.0 {
958+
return Err(MethodError::NoMatch(NoMatchData {
959+
static_candidates: vec![],
960+
unsatisfied_predicates: vec![],
961+
out_of_scope_traits: vec![],
962+
similar_candidate: None,
963+
mode: self.mode,
964+
}));
965+
}
966+
947967
debug!("pick: actual search failed, assemble diagnostics");
948968

949969
let static_candidates = std::mem::take(self.static_candidates.get_mut());
@@ -1631,6 +1651,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16311651
self.orig_steps_var_values,
16321652
self.steps,
16331653
self.scope_expr_id,
1654+
IsSuggestion(true),
16341655
);
16351656
pcx.allow_similar_names = true;
16361657
pcx.assemble_inherent_candidates();

compiler/rustc_hir_typeck/src/method/suggest.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11431143
}
11441144
}
11451145

1146-
let label_span_not_found = |err: &mut Diag<'_>| {
1146+
let mut find_candidate_for_method = false;
1147+
1148+
let mut label_span_not_found = |err: &mut Diag<'_>| {
11471149
if unsatisfied_predicates.is_empty() {
11481150
err.span_label(span, format!("{item_kind} not found in `{ty_str}`"));
11491151
let is_string_or_ref_str = match rcvr_ty.kind() {
@@ -1219,6 +1221,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12191221
err.note(format!(
12201222
"the {item_kind} was found for\n{type_candidates}{additional_types}"
12211223
));
1224+
find_candidate_for_method = mode == Mode::MethodCall;
12221225
}
12231226
}
12241227
} else {
@@ -1371,9 +1374,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13711374
);
13721375
}
13731376
}
1374-
// If an appropriate error source is not found, check method chain for possible candiates
1375-
if unsatisfied_predicates.is_empty()
1376-
&& let Mode::MethodCall = mode
1377+
1378+
if !find_candidate_for_method {
1379+
self.lookup_segments_chain_for_no_match_method(
1380+
&mut err,
1381+
item_name,
1382+
item_kind,
1383+
source,
1384+
no_match_data,
1385+
);
1386+
}
1387+
1388+
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
1389+
Some(err)
1390+
}
1391+
1392+
/// If an appropriate error source is not found, check method chain for possible candidates
1393+
fn lookup_segments_chain_for_no_match_method(
1394+
&self,
1395+
err: &mut Diag<'_>,
1396+
item_name: Ident,
1397+
item_kind: &str,
1398+
source: SelfSource<'tcx>,
1399+
no_match_data: &NoMatchData<'tcx>,
1400+
) {
1401+
if no_match_data.unsatisfied_predicates.is_empty()
1402+
&& let Mode::MethodCall = no_match_data.mode
13771403
&& let SelfSource::MethodCall(mut source_expr) = source
13781404
{
13791405
let mut stack_methods = vec![];
@@ -1394,6 +1420,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13941420
.unwrap_or(Ty::new_misc_error(self.tcx)),
13951421
);
13961422

1423+
// FIXME: `probe_for_name_many` searches for methods in inherent implementations,
1424+
// so it may return a candidate that doesn't belong to this `revr_ty`. We need to
1425+
// check whether the instantiated type matches the received one.
13971426
for _matched_method in self.probe_for_name_many(
13981427
Mode::MethodCall,
13991428
item_name,
@@ -1416,8 +1445,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14161445
);
14171446
}
14181447
}
1419-
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
1420-
Some(err)
14211448
}
14221449

14231450
fn find_likely_intended_associated_item(
@@ -2814,7 +2841,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28142841
Some(output_ty) => self.resolve_vars_if_possible(output_ty),
28152842
_ => return,
28162843
};
2817-
let method_exists = self.method_exists(item_name, output_ty, call.hir_id, return_type);
2844+
let method_exists =
2845+
self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
28182846
debug!("suggest_await_before_method: is_method_exist={}", method_exists);
28192847
if method_exists {
28202848
err.span_suggestion_verbose(

compiler/rustc_middle/src/traits/solve/inspect.rs

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ pub enum ProbeKind<'tcx> {
153153
/// do a probe to find out what projection type(s) may be used to prove that
154154
/// the source type upholds all of the target type's object bounds.
155155
UpcastProjectionCompatibility,
156+
/// Looking for param-env candidates that satisfy the trait ref for a projection.
157+
ShadowedEnvProbing,
156158
/// Try to unify an opaque type with an existing key in the storage.
157159
OpaqueTypeStorageLookup { result: QueryResult<'tcx> },
158160
}

compiler/rustc_middle/src/traits/solve/inspect/format.rs

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
118118
ProbeKind::TraitCandidate { source, result } => {
119119
write!(self.f, "CANDIDATE {source:?}: {result:?}")
120120
}
121+
ProbeKind::ShadowedEnvProbing => {
122+
write!(self.f, "PROBING FOR IMPLS SHADOWED BY PARAM-ENV CANDIDATE:")
123+
}
121124
}?;
122125

123126
self.nested(|this| {

compiler/rustc_middle/src/ty/consts.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use rustc_hir as hir;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
10-
use rustc_type_ir::ConstKind as IrConstKind;
11-
use rustc_type_ir::{TypeFlags, WithCachedTypeInfo};
10+
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
1211

1312
mod int;
1413
mod kind;
@@ -20,7 +19,8 @@ use rustc_span::Span;
2019
use rustc_span::DUMMY_SP;
2120
pub use valtree::*;
2221

23-
pub type ConstKind<'tcx> = IrConstKind<TyCtxt<'tcx>>;
22+
pub type ConstKind<'tcx> = ir::ConstKind<TyCtxt<'tcx>>;
23+
pub type UnevaluatedConst<'tcx> = ir::UnevaluatedConst<TyCtxt<'tcx>>;
2424

2525
#[cfg(target_pointer_width = "64")]
2626
rustc_data_structures::static_assert_size!(ConstKind<'_>, 32);
@@ -184,6 +184,14 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
184184
Const::new_bound(tcx, debruijn, var, ty)
185185
}
186186

187+
fn new_unevaluated(
188+
interner: TyCtxt<'tcx>,
189+
uv: ty::UnevaluatedConst<'tcx>,
190+
ty: Ty<'tcx>,
191+
) -> Self {
192+
Const::new_unevaluated(interner, uv, ty)
193+
}
194+
187195
fn ty(self) -> Ty<'tcx> {
188196
self.ty()
189197
}

compiler/rustc_middle/src/ty/consts/kind.rs

+4-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
11
use super::Const;
22
use crate::mir;
33
use crate::ty::abstract_const::CastKind;
4-
use crate::ty::GenericArgsRef;
54
use crate::ty::{self, visit::TypeVisitableExt as _, List, Ty, TyCtxt};
6-
use rustc_hir::def_id::DefId;
7-
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
5+
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
86

9-
/// An unevaluated (potentially generic) constant used in the type-system.
10-
#[derive(Copy, Clone, Eq, PartialEq, TyEncodable, TyDecodable)]
11-
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
12-
pub struct UnevaluatedConst<'tcx> {
13-
pub def: DefId,
14-
pub args: GenericArgsRef<'tcx>,
15-
}
16-
17-
impl rustc_errors::IntoDiagArg for UnevaluatedConst<'_> {
18-
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
19-
format!("{self:?}").into_diag_arg()
20-
}
21-
}
22-
23-
impl<'tcx> UnevaluatedConst<'tcx> {
7+
#[extension(pub(crate) trait UnevaluatedConstEvalExt<'tcx>)]
8+
impl<'tcx> ty::UnevaluatedConst<'tcx> {
249
/// FIXME(RalfJung): I cannot explain what this does or why it makes sense, but not doing this
2510
/// hurts performance.
2611
#[inline]
27-
pub(crate) fn prepare_for_eval(
12+
fn prepare_for_eval(
2813
self,
2914
tcx: TyCtxt<'tcx>,
3015
param_env: ty::ParamEnv<'tcx>,
@@ -55,13 +40,6 @@ impl<'tcx> UnevaluatedConst<'tcx> {
5540
}
5641
}
5742

58-
impl<'tcx> UnevaluatedConst<'tcx> {
59-
#[inline]
60-
pub fn new(def: DefId, args: GenericArgsRef<'tcx>) -> UnevaluatedConst<'tcx> {
61-
UnevaluatedConst { def, args }
62-
}
63-
}
64-
6543
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
6644
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
6745
pub enum Expr<'tcx> {

0 commit comments

Comments
 (0)