@@ -5,14 +5,14 @@ use rustc_errors::codes::*;
55use rustc_errors:: struct_span_code_err;
66use rustc_hir as hir;
77use rustc_hir:: def:: { DefKind , Res } ;
8- use rustc_hir:: def_id:: { DefId , LocalDefId } ;
8+ use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LocalDefId } ;
99use rustc_hir:: { AmbigArg , LangItem , PolyTraitRef } ;
1010use rustc_middle:: bug;
1111use rustc_middle:: ty:: {
1212 self as ty, IsSuggestable , Ty , TyCtxt , TypeSuperVisitable , TypeVisitable , TypeVisitableExt ,
1313 TypeVisitor , Upcast ,
1414} ;
15- use rustc_span:: { ErrorGuaranteed , Ident , Span , Symbol , kw} ;
15+ use rustc_span:: { ErrorGuaranteed , Ident , Span , Symbol , kw, sym } ;
1616use rustc_trait_selection:: traits;
1717use smallvec:: SmallVec ;
1818use tracing:: { debug, instrument} ;
@@ -191,6 +191,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
191191 let meta_sized_did = tcx. require_lang_item ( LangItem :: MetaSized , span) ;
192192 let pointee_sized_did = tcx. require_lang_item ( LangItem :: PointeeSized , span) ;
193193
194+ // Skip adding any default bounds if `#![rustc_no_implicit_bounds]`
195+ if tcx. has_attr ( CRATE_DEF_ID , sym:: rustc_no_implicit_bounds) {
196+ return ;
197+ }
198+
194199 // If adding sizedness bounds to a trait, then there are some relevant early exits
195200 if let Some ( trait_did) = trait_did {
196201 let trait_did = trait_did. to_def_id ( ) ;
@@ -408,7 +413,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
408413 let tcx = self . tcx ( ) ;
409414 let trait_id = tcx. lang_items ( ) . get ( trait_) ;
410415 if let Some ( trait_id) = trait_id
411- && self . do_not_provide_default_trait_bound (
416+ && ! self . do_not_provide_default_trait_bound (
412417 trait_id,
413418 hir_bounds,
414419 self_ty_where_predicates,
@@ -418,14 +423,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
418423 }
419424 }
420425
426+ /// Returns `true` if default trait bound should not be added.
421427 fn do_not_provide_default_trait_bound < ' a > (
422428 & self ,
423429 trait_def_id : DefId ,
424430 hir_bounds : & ' a [ hir:: GenericBound < ' tcx > ] ,
425431 self_ty_where_predicates : Option < ( LocalDefId , & ' tcx [ hir:: WherePredicate < ' tcx > ] ) > ,
426432 ) -> bool {
427433 let collected = collect_bounds ( hir_bounds, self_ty_where_predicates, trait_def_id) ;
428- ! collected. any ( )
434+ self . tcx ( ) . has_attr ( CRATE_DEF_ID , sym :: rustc_no_implicit_bounds ) || collected. any ( )
429435 }
430436
431437 /// Lower HIR bounds into `bounds` given the self type `param_ty` and the overarching late-bound vars if any.
0 commit comments