Skip to content

Commit d9140be

Browse files
committed
review comments
- Pass `ParamEnv` through - Remove now-unnecessary `Formatter` mode - Rework the way we pick up the bounds
1 parent d113a09 commit d9140be

File tree

7 files changed

+70
-27
lines changed

7 files changed

+70
-27
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15391539
ty.into(),
15401540
TypeAnnotationNeeded::E0282,
15411541
true,
1542+
self.param_env,
15421543
)
15431544
.emit()
15441545
});

compiler/rustc_hir_typeck/src/method/probe.rs

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
435435
ty.into(),
436436
TypeAnnotationNeeded::E0282,
437437
!raw_ptr_call,
438+
self.param_env,
438439
);
439440
if raw_ptr_call {
440441
err.span_label(span, "cannot call a method on a raw pointer with an unknown pointee type");

compiler/rustc_hir_typeck/src/writeback.rs

+1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
784784
p.into(),
785785
TypeAnnotationNeeded::E0282,
786786
false,
787+
self.fcx.param_env,
787788
)
788789
.emit()
789790
}

compiler/rustc_middle/src/ty/print/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub trait Printer<'tcx>: Sized {
227227
Some(trait_ref) => self.tcx().parent(trait_ref.def_id) == parent_def_id,
228228
};
229229

230-
if !in_self_mod && !in_trait_mod && !self.for_suggestion() {
230+
if !in_self_mod && !in_trait_mod {
231231
// If the impl is not co-located with either self-type or
232232
// trait-type, then fallback to a format that identifies
233233
// the module more clearly.
@@ -243,10 +243,6 @@ pub trait Printer<'tcx>: Sized {
243243
self.path_qualified(self_ty, impl_trait_ref)
244244
}
245245
}
246-
247-
fn for_suggestion(&self) -> bool {
248-
false
249-
}
250246
}
251247

252248
/// As a heuristic, when we see an impl, if we see that the

compiler/rustc_middle/src/ty/print/pretty.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,6 @@ pub struct FmtPrinterData<'a, 'tcx> {
20102010

20112011
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<Symbol> + 'a>>,
20122012
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid) -> Option<Symbol> + 'a>>,
2013-
pub for_suggestion: bool,
20142013
}
20152014

20162015
impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> {
@@ -2061,7 +2060,6 @@ impl<'a, 'tcx> FmtPrinter<'a, 'tcx> {
20612060
region_highlight_mode: RegionHighlightMode::default(),
20622061
ty_infer_name_resolver: None,
20632062
const_infer_name_resolver: None,
2064-
for_suggestion: false,
20652063
}))
20662064
}
20672065

@@ -2313,9 +2311,6 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
23132311
Ok(())
23142312
}
23152313
}
2316-
fn for_suggestion(&self) -> bool {
2317-
self.0.for_suggestion
2318-
}
23192314
}
23202315

23212316
impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

+59-17
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
205205
}
206206
};
207207
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
208-
printer.for_suggestion = true;
209208
let const_getter =
210209
move |ct_vid| Some(infcx.tcx.item_name(infcx.const_var_origin(ct_vid)?.param_def_id?));
211210
printer.const_infer_name_resolver = Some(Box::new(const_getter));
@@ -419,6 +418,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
419418
arg: GenericArg<'tcx>,
420419
error_code: TypeAnnotationNeeded,
421420
should_label_span: bool,
421+
param_env: ty::ParamEnv<'tcx>,
422422
) -> Diag<'a> {
423423
let arg = self.resolve_vars_if_possible(arg);
424424
let arg_data = self.extract_inference_diagnostics_data(arg, None);
@@ -454,7 +454,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
454454
match kind {
455455
InferSourceKind::LetBinding { insert_span, pattern_name, ty, def_id, hir_id } => {
456456
let mut paths = vec![];
457-
let param_env = ty::ParamEnv::reveal_all();
458457
if let Some(def_id) = def_id
459458
&& let name = self.infcx.tcx.item_name(def_id)
460459
&& let Some(hir_id) = hir_id
@@ -468,7 +467,25 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
468467
self.infcx.tcx.parent(def_id), // Trait `DefId`
469468
ty, // `Self` type
470469
|impl_def_id| {
471-
let impl_args = self.fresh_args_for_item(DUMMY_SP, impl_def_id);
470+
let impl_args = ty::GenericArgs::for_item(
471+
self.infcx.tcx,
472+
impl_def_id,
473+
|param, _| {
474+
// We don't want to name the arguments, we just want to give an
475+
// idea of what the syntax is.
476+
match param.kind {
477+
ty::GenericParamDefKind::Lifetime => {
478+
self.infcx.tcx.lifetimes.re_erased.into()
479+
}
480+
ty::GenericParamDefKind::Type { .. } => {
481+
self.next_ty_var(DUMMY_SP).into()
482+
}
483+
ty::GenericParamDefKind::Const { .. } => {
484+
self.next_const_var(DUMMY_SP).into()
485+
}
486+
}
487+
},
488+
);
472489
let impl_trait_ref = self
473490
.infcx
474491
.tcx
@@ -521,15 +538,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
521538
// The method isn't in this `impl`? Not useful to us then.
522539
return;
523540
};
541+
let Some(trait_assoc_item) = assoc.trait_item_def_id else {
542+
return;
543+
};
524544
// Let's ignore the generic params and replace them with `_` in the
525545
// suggested path.
526-
let identity_method = ty::GenericArgs::for_item(
546+
let trait_assoc_substs = impl_trait_ref.args.extend_to(
527547
self.infcx.tcx,
528-
assoc.def_id,
529-
|param, _| {
548+
trait_assoc_item,
549+
|def, _| {
530550
// We don't want to name the arguments, we just want to give an
531551
// idea of what the syntax is.
532-
match param.kind {
552+
match def.kind {
533553
ty::GenericParamDefKind::Lifetime => {
534554
self.infcx.tcx.lifetimes.re_erased.into()
535555
}
@@ -542,10 +562,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
542562
}
543563
},
544564
);
565+
let identity_method =
566+
impl_args.rebase_onto(self.infcx.tcx, def_id, trait_assoc_substs);
545567
let fn_sig = self
546568
.infcx
547569
.tcx
548-
.fn_sig(assoc.def_id)
570+
.fn_sig(def_id)
549571
.instantiate(self.infcx.tcx, identity_method);
550572
let ret = fn_sig.skip_binder().output();
551573
paths.push(format!("{ret}"));
@@ -676,15 +698,32 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
676698
};
677699

678700
let mut paths = vec![];
679-
let param_env = ty::ParamEnv::reveal_all();
680701
let name = self.infcx.tcx.item_name(def_id);
681702
// Look for all the possible implementations to suggest, otherwise we'll show
682703
// just suggest the syntax for the fully qualified path with placeholders.
683704
self.infcx.tcx.for_each_relevant_impl(
684705
self.infcx.tcx.parent(def_id), // Trait `DefId`
685706
args.type_at(0), // `Self` type
686707
|impl_def_id| {
687-
let impl_args = self.fresh_args_for_item(DUMMY_SP, impl_def_id);
708+
let impl_args = ty::GenericArgs::for_item(
709+
self.infcx.tcx,
710+
impl_def_id,
711+
|param, _| {
712+
// We don't want to name the arguments, we just want to give an
713+
// idea of what the syntax is.
714+
match param.kind {
715+
ty::GenericParamDefKind::Lifetime => {
716+
self.infcx.tcx.lifetimes.re_erased.into()
717+
}
718+
ty::GenericParamDefKind::Type { .. } => {
719+
self.next_ty_var(DUMMY_SP).into()
720+
}
721+
ty::GenericParamDefKind::Const { .. } => {
722+
self.next_const_var(DUMMY_SP).into()
723+
}
724+
}
725+
},
726+
);
688727
let impl_trait_ref = self
689728
.infcx
690729
.tcx
@@ -737,15 +776,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
737776
// The method isn't in this `impl`? Not useful to us then.
738777
return;
739778
};
740-
// Let's ignore the generic params and replace them with `_` in the
741-
// suggested path.
742-
let identity_method = ty::GenericArgs::for_item(
779+
let Some(trait_assoc_item) = assoc.trait_item_def_id else {
780+
return;
781+
};
782+
let trait_assoc_substs = impl_trait_ref.args.extend_to(
743783
self.infcx.tcx,
744-
assoc.def_id,
745-
|param, _| {
784+
trait_assoc_item,
785+
|def, _| {
746786
// We don't want to name the arguments, we just want to give an
747787
// idea of what the syntax is.
748-
match param.kind {
788+
match def.kind {
749789
ty::GenericParamDefKind::Lifetime => {
750790
self.infcx.tcx.lifetimes.re_erased.into()
751791
}
@@ -758,8 +798,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
758798
}
759799
},
760800
);
801+
let identity_method =
802+
args.rebase_onto(self.infcx.tcx, def_id, trait_assoc_substs);
761803
let mut printer = fmt_printer(self, Namespace::ValueNS);
762-
printer.print_def_path(assoc.def_id, identity_method).unwrap();
804+
printer.print_def_path(def_id, identity_method).unwrap();
763805
paths.push(printer.into_buffer());
764806
},
765807
);

compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs

+7
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
208208
trait_ref.self_ty().skip_binder().into(),
209209
TypeAnnotationNeeded::E0282,
210210
false,
211+
obligation.param_env,
211212
);
212213
return err.stash(span, StashKey::MaybeForgetReturn).unwrap();
213214
}
@@ -237,6 +238,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
237238
arg,
238239
TypeAnnotationNeeded::E0283,
239240
true,
241+
obligation.param_env,
240242
)
241243
} else {
242244
struct_span_code_err!(
@@ -477,6 +479,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
477479
arg,
478480
TypeAnnotationNeeded::E0282,
479481
false,
482+
obligation.param_env,
480483
)
481484
}
482485

@@ -496,6 +499,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
496499
a.into(),
497500
TypeAnnotationNeeded::E0282,
498501
true,
502+
obligation.param_env,
499503
)
500504
}
501505
ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
@@ -526,6 +530,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
526530
arg,
527531
TypeAnnotationNeeded::E0284,
528532
true,
533+
obligation.param_env,
529534
)
530535
.with_note(format!("cannot satisfy `{predicate}`"))
531536
} else {
@@ -556,6 +561,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
556561
arg,
557562
TypeAnnotationNeeded::E0284,
558563
true,
564+
obligation.param_env,
559565
);
560566
err
561567
} else {
@@ -578,6 +584,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
578584
ct.into(),
579585
TypeAnnotationNeeded::E0284,
580586
true,
587+
obligation.param_env,
581588
),
582589
ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term })
583590
if term.is_infer() =>

0 commit comments

Comments
 (0)