From 94ca92c76b67abda45b16d9f0fbffebeb9a6b481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 5 Aug 2024 21:28:40 +0000 Subject: [PATCH] review comments - Pass `ParamEnv` through - Remove now-unnecessary `Formatter` mode - Rework the way we pick up the bounds --- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 1 + compiler/rustc_hir_typeck/src/method/probe.rs | 1 + compiler/rustc_hir_typeck/src/writeback.rs | 1 + compiler/rustc_middle/src/ty/print/mod.rs | 6 +- compiler/rustc_middle/src/ty/print/pretty.rs | 5 -- .../error_reporting/infer/need_type_info.rs | 76 ++++++++++++++----- .../src/error_reporting/traits/ambiguity.rs | 7 ++ 7 files changed, 70 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 40d9a2985da06..63ef77a41c48c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -1539,6 +1539,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty.into(), TypeAnnotationNeeded::E0282, true, + self.param_env, ) .emit() }); diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 82f732d69dc52..c803102b44d43 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -435,6 +435,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty.into(), TypeAnnotationNeeded::E0282, !raw_ptr_call, + self.param_env, ); if raw_ptr_call { err.span_label(span, "cannot call a method on a raw pointer with an unknown pointee type"); diff --git a/compiler/rustc_hir_typeck/src/writeback.rs b/compiler/rustc_hir_typeck/src/writeback.rs index 0327a3097eca2..bcde1547e1a2b 100644 --- a/compiler/rustc_hir_typeck/src/writeback.rs +++ b/compiler/rustc_hir_typeck/src/writeback.rs @@ -784,6 +784,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> { p.into(), TypeAnnotationNeeded::E0282, false, + self.fcx.param_env, ) .emit() } diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index 0186d54d2e6d3..6cce79dfdc1cf 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -227,7 +227,7 @@ pub trait Printer<'tcx>: Sized { Some(trait_ref) => self.tcx().parent(trait_ref.def_id) == parent_def_id, }; - if !in_self_mod && !in_trait_mod && !self.for_suggestion() { + if !in_self_mod && !in_trait_mod { // If the impl is not co-located with either self-type or // trait-type, then fallback to a format that identifies // the module more clearly. @@ -243,10 +243,6 @@ pub trait Printer<'tcx>: Sized { self.path_qualified(self_ty, impl_trait_ref) } } - - fn for_suggestion(&self) -> bool { - false - } } /// As a heuristic, when we see an impl, if we see that the diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 6a4f6736fd768..29d72183dd3ff 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2010,7 +2010,6 @@ pub struct FmtPrinterData<'a, 'tcx> { pub ty_infer_name_resolver: Option Option + 'a>>, pub const_infer_name_resolver: Option Option + 'a>>, - pub for_suggestion: bool, } impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> { @@ -2061,7 +2060,6 @@ impl<'a, 'tcx> FmtPrinter<'a, 'tcx> { region_highlight_mode: RegionHighlightMode::default(), ty_infer_name_resolver: None, const_infer_name_resolver: None, - for_suggestion: false, })) } @@ -2313,9 +2311,6 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { Ok(()) } } - fn for_suggestion(&self) -> bool { - self.0.for_suggestion - } } impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index 4731cc85034c2..6da98c46ddcab 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -205,7 +205,6 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte } }; printer.ty_infer_name_resolver = Some(Box::new(ty_getter)); - printer.for_suggestion = true; let const_getter = move |ct_vid| Some(infcx.tcx.item_name(infcx.const_var_origin(ct_vid)?.param_def_id?)); printer.const_infer_name_resolver = Some(Box::new(const_getter)); @@ -419,6 +418,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { arg: GenericArg<'tcx>, error_code: TypeAnnotationNeeded, should_label_span: bool, + param_env: ty::ParamEnv<'tcx>, ) -> Diag<'a> { let arg = self.resolve_vars_if_possible(arg); let arg_data = self.extract_inference_diagnostics_data(arg, None); @@ -454,7 +454,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { match kind { InferSourceKind::LetBinding { insert_span, pattern_name, ty, def_id, hir_id } => { let mut paths = vec![]; - let param_env = ty::ParamEnv::reveal_all(); if let Some(def_id) = def_id && let name = self.infcx.tcx.item_name(def_id) && let Some(hir_id) = hir_id @@ -468,7 +467,25 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { self.infcx.tcx.parent(def_id), // Trait `DefId` ty, // `Self` type |impl_def_id| { - let impl_args = self.fresh_args_for_item(DUMMY_SP, impl_def_id); + let impl_args = ty::GenericArgs::for_item( + self.infcx.tcx, + impl_def_id, + |param, _| { + // We don't want to name the arguments, we just want to give an + // idea of what the syntax is. + match param.kind { + ty::GenericParamDefKind::Lifetime => { + self.infcx.tcx.lifetimes.re_erased.into() + } + ty::GenericParamDefKind::Type { .. } => { + self.next_ty_var(DUMMY_SP).into() + } + ty::GenericParamDefKind::Const { .. } => { + self.next_const_var(DUMMY_SP).into() + } + } + }, + ); let impl_trait_ref = self .infcx .tcx @@ -521,15 +538,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // The method isn't in this `impl`? Not useful to us then. return; }; + let Some(trait_assoc_item) = assoc.trait_item_def_id else { + return; + }; // Let's ignore the generic params and replace them with `_` in the // suggested path. - let identity_method = ty::GenericArgs::for_item( + let trait_assoc_substs = impl_trait_ref.args.extend_to( self.infcx.tcx, - assoc.def_id, - |param, _| { + trait_assoc_item, + |def, _| { // We don't want to name the arguments, we just want to give an // idea of what the syntax is. - match param.kind { + match def.kind { ty::GenericParamDefKind::Lifetime => { self.infcx.tcx.lifetimes.re_erased.into() } @@ -542,10 +562,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } }, ); + let identity_method = + impl_args.rebase_onto(self.infcx.tcx, def_id, trait_assoc_substs); let fn_sig = self .infcx .tcx - .fn_sig(assoc.def_id) + .fn_sig(def_id) .instantiate(self.infcx.tcx, identity_method); let ret = fn_sig.skip_binder().output(); paths.push(format!("{ret}")); @@ -676,7 +698,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { }; let mut paths = vec![]; - let param_env = ty::ParamEnv::reveal_all(); let name = self.infcx.tcx.item_name(def_id); // Look for all the possible implementations to suggest, otherwise we'll show // just suggest the syntax for the fully qualified path with placeholders. @@ -684,7 +705,25 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { self.infcx.tcx.parent(def_id), // Trait `DefId` args.type_at(0), // `Self` type |impl_def_id| { - let impl_args = self.fresh_args_for_item(DUMMY_SP, impl_def_id); + let impl_args = ty::GenericArgs::for_item( + self.infcx.tcx, + impl_def_id, + |param, _| { + // We don't want to name the arguments, we just want to give an + // idea of what the syntax is. + match param.kind { + ty::GenericParamDefKind::Lifetime => { + self.infcx.tcx.lifetimes.re_erased.into() + } + ty::GenericParamDefKind::Type { .. } => { + self.next_ty_var(DUMMY_SP).into() + } + ty::GenericParamDefKind::Const { .. } => { + self.next_const_var(DUMMY_SP).into() + } + } + }, + ); let impl_trait_ref = self .infcx .tcx @@ -737,15 +776,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // The method isn't in this `impl`? Not useful to us then. return; }; - // Let's ignore the generic params and replace them with `_` in the - // suggested path. - let identity_method = ty::GenericArgs::for_item( + let Some(trait_assoc_item) = assoc.trait_item_def_id else { + return; + }; + let trait_assoc_substs = impl_trait_ref.args.extend_to( self.infcx.tcx, - assoc.def_id, - |param, _| { + trait_assoc_item, + |def, _| { // We don't want to name the arguments, we just want to give an // idea of what the syntax is. - match param.kind { + match def.kind { ty::GenericParamDefKind::Lifetime => { self.infcx.tcx.lifetimes.re_erased.into() } @@ -758,8 +798,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } }, ); + let identity_method = + args.rebase_onto(self.infcx.tcx, def_id, trait_assoc_substs); let mut printer = fmt_printer(self, Namespace::ValueNS); - printer.print_def_path(assoc.def_id, identity_method).unwrap(); + printer.print_def_path(def_id, identity_method).unwrap(); paths.push(printer.into_buffer()); }, ); diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs index 72a4d4c120561..0ae5494f95eea 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs @@ -208,6 +208,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { trait_ref.self_ty().skip_binder().into(), TypeAnnotationNeeded::E0282, false, + obligation.param_env, ); return err.stash(span, StashKey::MaybeForgetReturn).unwrap(); } @@ -237,6 +238,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { arg, TypeAnnotationNeeded::E0283, true, + obligation.param_env, ) } else { struct_span_code_err!( @@ -449,6 +451,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { arg, TypeAnnotationNeeded::E0282, false, + obligation.param_env, ) } @@ -468,6 +471,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { a.into(), TypeAnnotationNeeded::E0282, true, + obligation.param_env, ) } ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => { @@ -498,6 +502,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { arg, TypeAnnotationNeeded::E0284, true, + obligation.param_env, ) .with_note(format!("cannot satisfy `{predicate}`")) } else { @@ -528,6 +533,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { arg, TypeAnnotationNeeded::E0284, true, + obligation.param_env, ); err } else { @@ -550,6 +556,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ct.into(), TypeAnnotationNeeded::E0284, true, + obligation.param_env, ), ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term }) if term.is_infer() =>