diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 404e970403e..a705d3bc107 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -372,23 +372,14 @@ fn item_def_id(&self) -> DefId { self.item_def_id.to_def_id() } - fn probe_ty_param_bounds( - &self, - span: Span, - def_id: LocalDefId, - assoc_name: Ident, - ) -> ty::GenericPredicates<'tcx> { - self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name)) + fn allow_infer(&self) -> bool { + false } fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option> { None } - fn allow_infer(&self) -> bool { - false - } - fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> { Ty::new_error_with_message(self.tcx(), span, "bad placeholder type") } @@ -403,6 +394,15 @@ fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> ty::Const::new_error_with_message(self.tcx(), ty, span, "bad placeholder constant") } + fn probe_ty_param_bounds( + &self, + span: Span, + def_id: LocalDefId, + assoc_name: Ident, + ) -> ty::GenericPredicates<'tcx> { + self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name)) + } + fn lower_assoc_ty( &self, span: Span, @@ -496,10 +496,6 @@ fn probe_adt(&self, _span: Span, ty: Ty<'tcx>) -> Option> { ty.ty_adt_def() } - fn set_tainted_by_errors(&self, err: ErrorGuaranteed) { - self.tainted_by_errors.set(Some(err)); - } - fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) { // There's no place to record types from signatures? } @@ -507,6 +503,10 @@ fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) { fn infcx(&self) -> Option<&InferCtxt<'tcx>> { None } + + fn set_tainted_by_errors(&self, err: ErrorGuaranteed) { + self.tainted_by_errors.set(Some(err)); + } } /// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present. diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index aef697c658d..109e00d4f24 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -89,6 +89,24 @@ pub trait HirTyLowerer<'tcx> { /// Returns the [`DefId`] of the overarching item whose constituents get lowered. fn item_def_id(&self) -> DefId; + /// Returns `true` if the current context allows the use of inference variables. + fn allow_infer(&self) -> bool; + + /// Returns the region to use when a lifetime is omitted (and not elided). + fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) + -> Option>; + + /// Returns the type to use when a type is omitted. + fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>; + + /// Returns the const to use when a const is omitted. + fn ct_infer( + &self, + ty: Ty<'tcx>, + param: Option<&ty::GenericParamDef>, + span: Span, + ) -> Const<'tcx>; + /// Probe bounds in scope where the bounded type coincides with the given type parameter. /// /// Rephrased, this returns bounds of the form `T: Trait`, where `T` is a type parameter @@ -110,24 +128,6 @@ fn probe_ty_param_bounds( assoc_name: Ident, ) -> ty::GenericPredicates<'tcx>; - /// Returns the region to use when a lifetime is omitted (and not elided). - fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) - -> Option>; - - /// Returns the type to use when a type is omitted. - fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>; - - /// Returns `true` if the current context allows the use of inference variables. - fn allow_infer(&self) -> bool; - - /// Returns the const to use when a const is omitted. - fn ct_infer( - &self, - ty: Ty<'tcx>, - param: Option<&ty::GenericParamDef>, - span: Span, - ) -> Const<'tcx>; - /// Lower an associated type to a projection. /// /// This method has to be defined by the concrete lowering context because @@ -156,15 +156,18 @@ fn lower_assoc_ty( /// or to an enum variant depending on the result of this function. fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option>; + /// Record the lowered type of a HIR node in this context. + fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span); + + /// The inference context of the lowering context if applicable. + fn infcx(&self) -> Option<&InferCtxt<'tcx>>; + /// Taint the context with errors. /// /// Invoke this when you encounter an error from some prior pass like name resolution. /// This is used to help suppress derived errors typeck might otherwise report. fn set_tainted_by_errors(&self, e: ErrorGuaranteed); - /// Record the lowered type of a HIR node in this context. - fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span); - /// Convenience method for coercing the lowering context into a trait object type. /// /// Most lowering routines are defined on the trait object type directly @@ -175,9 +178,6 @@ fn lowerer(&self) -> &dyn HirTyLowerer<'tcx> { self } - - /// The inference context of the lowering context if applicable. - fn infcx(&self) -> Option<&InferCtxt<'tcx>>; } /// New-typed boolean indicating whether explicit late-bound lifetimes diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs index a83a1169c98..efa2862177e 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs @@ -221,31 +221,8 @@ fn item_def_id(&self) -> DefId { self.body_id.to_def_id() } - fn probe_ty_param_bounds( - &self, - _: Span, - def_id: LocalDefId, - _: Ident, - ) -> ty::GenericPredicates<'tcx> { - let tcx = self.tcx; - let item_def_id = tcx.hir().ty_param_owner(def_id); - let generics = tcx.generics_of(item_def_id); - let index = generics.param_def_id_to_index[&def_id.to_def_id()]; - // HACK(eddyb) should get the original `Span`. - let span = tcx.def_span(def_id); - ty::GenericPredicates { - parent: None, - predicates: tcx.arena.alloc_from_iter( - self.param_env.caller_bounds().iter().filter_map(|predicate| { - match predicate.kind().skip_binder() { - ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => { - Some((predicate, span)) - } - _ => None, - } - }), - ), - } + fn allow_infer(&self) -> bool { + true } fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option> { @@ -256,10 +233,6 @@ fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option bool { - true - } - fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> { match param { Some(param) => self.var_for_def(span, param).as_type().unwrap(), @@ -292,6 +265,33 @@ fn ct_infer( } } + fn probe_ty_param_bounds( + &self, + _: Span, + def_id: LocalDefId, + _: Ident, + ) -> ty::GenericPredicates<'tcx> { + let tcx = self.tcx; + let item_def_id = tcx.hir().ty_param_owner(def_id); + let generics = tcx.generics_of(item_def_id); + let index = generics.param_def_id_to_index[&def_id.to_def_id()]; + // HACK(eddyb) should get the original `Span`. + let span = tcx.def_span(def_id); + ty::GenericPredicates { + parent: None, + predicates: tcx.arena.alloc_from_iter( + self.param_env.caller_bounds().iter().filter_map(|predicate| { + match predicate.kind().skip_binder() { + ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => { + Some((predicate, span)) + } + _ => None, + } + }), + ), + } + } + fn lower_assoc_ty( &self, span: Span, @@ -328,10 +328,6 @@ fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option> { } } - fn set_tainted_by_errors(&self, e: ErrorGuaranteed) { - self.infcx.set_tainted_by_errors(e) - } - fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span) { // FIXME: normalization and escaping regions let ty = if !ty.has_escaping_bound_vars() { @@ -355,6 +351,10 @@ fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span) { fn infcx(&self) -> Option<&infer::InferCtxt<'tcx>> { Some(&self.infcx) } + + fn set_tainted_by_errors(&self, e: ErrorGuaranteed) { + self.infcx.set_tainted_by_errors(e) + } } /// The `ty` representation of a user-provided type. Depending on the use-site