Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
 - Pass `ParamEnv` through
 - Remove now-unnecessary `Formatter` mode
 - Rework the way we pick up the bounds
  • Loading branch information
estebank committed Aug 5, 2024
1 parent f1e6f94 commit 94ca92c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 27 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty.into(),
TypeAnnotationNeeded::E0282,
true,
self.param_env,
)
.emit()
});
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
p.into(),
TypeAnnotationNeeded::E0282,
false,
self.fcx.param_env,
)
.emit()
}
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,6 @@ pub struct FmtPrinterData<'a, 'tcx> {

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

impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> {
Expand Down Expand Up @@ -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,
}))
}

Expand Down Expand Up @@ -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> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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}"));
Expand Down Expand Up @@ -676,15 +698,32 @@ 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.
self.infcx.tcx.for_each_relevant_impl(
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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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());
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -237,6 +238,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
arg,
TypeAnnotationNeeded::E0283,
true,
obligation.param_env,
)
} else {
struct_span_code_err!(
Expand Down Expand Up @@ -449,6 +451,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
arg,
TypeAnnotationNeeded::E0282,
false,
obligation.param_env,
)
}

Expand All @@ -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)) => {
Expand Down Expand Up @@ -498,6 +502,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
arg,
TypeAnnotationNeeded::E0284,
true,
obligation.param_env,
)
.with_note(format!("cannot satisfy `{predicate}`"))
} else {
Expand Down Expand Up @@ -528,6 +533,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
arg,
TypeAnnotationNeeded::E0284,
true,
obligation.param_env,
);
err
} else {
Expand All @@ -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() =>
Expand Down

0 comments on commit 94ca92c

Please sign in to comment.