1
1
use crate :: check:: regionck:: OutlivesEnvironmentExt ;
2
2
use crate :: check:: { FnCtxt , Inherited } ;
3
3
use crate :: constrained_generic_params:: { identify_constrained_generic_params, Parameter } ;
4
-
5
4
use rustc_ast as ast;
6
5
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
7
6
use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed } ;
@@ -13,6 +12,7 @@ use rustc_infer::infer::outlives::env::OutlivesEnvironment;
13
12
use rustc_infer:: infer:: outlives:: obligations:: TypeOutlives ;
14
13
use rustc_infer:: infer:: region_constraints:: GenericKind ;
15
14
use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
15
+ use rustc_infer:: traits:: Normalized ;
16
16
use rustc_middle:: ty:: query:: Providers ;
17
17
use rustc_middle:: ty:: subst:: { GenericArgKind , InternalSubsts , Subst } ;
18
18
use rustc_middle:: ty:: trait_def:: TraitSpecializationKind ;
@@ -24,7 +24,9 @@ use rustc_session::parse::feature_err;
24
24
use rustc_span:: symbol:: { sym, Ident , Symbol } ;
25
25
use rustc_span:: { Span , DUMMY_SP } ;
26
26
use rustc_trait_selection:: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
27
+ use rustc_trait_selection:: traits:: query:: normalize:: AtExt ;
27
28
use rustc_trait_selection:: traits:: { self , ObligationCause , ObligationCauseCode , WellFormedLoc } ;
29
+ use rustc_trait_selection:: traits:: query:: NoSolution ;
28
30
29
31
use std:: cell:: LazyCell ;
30
32
use std:: convert:: TryInto ;
@@ -939,9 +941,10 @@ fn check_associated_item(
939
941
940
942
let ( mut implied_bounds, self_ty) = match item. container {
941
943
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
+ ) ,
945
948
} ;
946
949
947
950
match item. kind {
@@ -1259,7 +1262,7 @@ fn check_impl<'tcx>(
1259
1262
1260
1263
check_where_clauses ( fcx, item. span , item. def_id , None ) ;
1261
1264
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 )
1263
1266
} ) ;
1264
1267
}
1265
1268
@@ -1917,28 +1920,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1917
1920
} )
1918
1921
. collect ( )
1919
1922
}
1923
+ }
1920
1924
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) {
1927
1938
Some ( trait_ref) => {
1928
1939
// Trait impl: take implied bounds from all types that
1929
1940
// 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
+ }
1932
1945
}
1933
1946
1934
1947
None => {
1935
1948
// 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
+ }
1939
1954
}
1940
1955
}
1941
- }
1956
+ } )
1942
1957
}
1943
1958
1944
1959
fn error_392 (
0 commit comments