11use crate :: check:: regionck:: OutlivesEnvironmentExt ;
22use crate :: check:: { FnCtxt , Inherited } ;
33use crate :: constrained_generic_params:: { identify_constrained_generic_params, Parameter } ;
4-
54use rustc_ast as ast;
65use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
76use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed } ;
@@ -13,6 +12,7 @@ use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1312use rustc_infer:: infer:: outlives:: obligations:: TypeOutlives ;
1413use rustc_infer:: infer:: region_constraints:: GenericKind ;
1514use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
15+ use rustc_infer:: traits:: Normalized ;
1616use rustc_middle:: ty:: query:: Providers ;
1717use rustc_middle:: ty:: subst:: { GenericArgKind , InternalSubsts , Subst } ;
1818use rustc_middle:: ty:: trait_def:: TraitSpecializationKind ;
@@ -24,7 +24,9 @@ use rustc_session::parse::feature_err;
2424use rustc_span:: symbol:: { sym, Ident , Symbol } ;
2525use rustc_span:: { Span , DUMMY_SP } ;
2626use rustc_trait_selection:: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
27+ use rustc_trait_selection:: traits:: query:: normalize:: AtExt ;
2728use rustc_trait_selection:: traits:: { self , ObligationCause , ObligationCauseCode , WellFormedLoc } ;
29+ use rustc_trait_selection:: traits:: query:: NoSolution ;
2830
2931use std:: cell:: LazyCell ;
3032use std:: convert:: TryInto ;
@@ -939,9 +941,10 @@ fn check_associated_item(
939941
940942 let ( mut implied_bounds, self_ty) = match item. container {
941943 ty:: TraitContainer ( _) => ( FxHashSet :: default ( ) , fcx. tcx . types . self_param ) ,
942- ty:: ImplContainer ( def_id) => {
943- ( fcx. impl_implied_bounds ( def_id, span) , fcx. tcx . type_of ( def_id) )
944- }
944+ ty:: ImplContainer ( def_id) => (
945+ impl_implied_bounds ( tcx, fcx. param_env , def_id. expect_local ( ) , span) ,
946+ fcx. tcx . type_of ( def_id) ,
947+ ) ,
945948 } ;
946949
947950 match item. kind {
@@ -1259,7 +1262,7 @@ fn check_impl<'tcx>(
12591262
12601263 check_where_clauses ( fcx, item. span , item. def_id , None ) ;
12611264
1262- fcx . impl_implied_bounds ( item. def_id . to_def_id ( ) , item. span )
1265+ impl_implied_bounds ( tcx , fcx . param_env , item. def_id , item. span )
12631266 } ) ;
12641267}
12651268
@@ -1917,28 +1920,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19171920 } )
19181921 . collect ( )
19191922 }
1923+ }
19201924
1921- pub ( super ) fn impl_implied_bounds (
1922- & self ,
1923- impl_def_id : DefId ,
1924- span : Span ,
1925- ) -> FxHashSet < Ty < ' tcx > > {
1926- match self . tcx . impl_trait_ref ( impl_def_id) {
1925+ pub ( super ) fn impl_implied_bounds < ' tcx > (
1926+ tcx : TyCtxt < ' tcx > ,
1927+ param_env : ty:: ParamEnv < ' tcx > ,
1928+ impl_def_id : LocalDefId ,
1929+ span : Span ,
1930+ ) -> FxHashSet < Ty < ' tcx > > {
1931+ // We completely ignore any obligations caused by normalizing the types
1932+ // we assume to be well formed. Considering that the user of the implied
1933+ // bounds will also normalize them, we leave it to them to emit errors
1934+ // which should result in better causes and spans.
1935+ tcx. infer_ctxt ( ) . enter ( |infcx| {
1936+ let cause = ObligationCause :: misc ( span, tcx. hir ( ) . local_def_id_to_hir_id ( impl_def_id) ) ;
1937+ match tcx. impl_trait_ref ( impl_def_id) {
19271938 Some ( trait_ref) => {
19281939 // Trait impl: take implied bounds from all types that
19291940 // appear in the trait reference.
1930- let trait_ref = self . normalize_associated_types_in ( span, trait_ref) ;
1931- trait_ref. substs . types ( ) . collect ( )
1941+ match infcx. at ( & cause, param_env) . normalize ( trait_ref) {
1942+ Ok ( Normalized { value, obligations : _ } ) => value. substs . types ( ) . collect ( ) ,
1943+ Err ( NoSolution ) => FxHashSet :: default ( ) ,
1944+ }
19321945 }
19331946
19341947 None => {
19351948 // Inherent impl: take implied bounds from the `self` type.
1936- let self_ty = self . tcx . type_of ( impl_def_id) ;
1937- let self_ty = self . normalize_associated_types_in ( span, self_ty) ;
1938- FxHashSet :: from_iter ( [ self_ty] )
1949+ let self_ty = tcx. type_of ( impl_def_id) ;
1950+ match infcx. at ( & cause, param_env) . normalize ( self_ty) {
1951+ Ok ( Normalized { value, obligations : _ } ) => FxHashSet :: from_iter ( [ value] ) ,
1952+ Err ( NoSolution ) => FxHashSet :: default ( ) ,
1953+ }
19391954 }
19401955 }
1941- }
1956+ } )
19421957}
19431958
19441959fn error_392 (
0 commit comments