@@ -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} ;
@@ -188,6 +188,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
188188 ) {
189189 let tcx = self . tcx ( ) ;
190190
191+ // Skip adding any default bounds if `#![rustc_no_implicit_bounds]`
192+ if tcx. has_attr ( CRATE_DEF_ID , sym:: rustc_no_implicit_bounds) {
193+ return ;
194+ }
195+
191196 let meta_sized_did = tcx. require_lang_item ( LangItem :: MetaSized , span) ;
192197 let pointee_sized_did = tcx. require_lang_item ( LangItem :: PointeeSized , span) ;
193198
@@ -408,24 +413,21 @@ 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 (
412- trait_id,
413- hir_bounds,
414- self_ty_where_predicates,
415- )
416+ && self . should_add_default_traits ( trait_id, hir_bounds, self_ty_where_predicates)
416417 {
417418 add_trait_bound ( tcx, bounds, self_ty, trait_id, span) ;
418419 }
419420 }
420421
421- fn do_not_provide_default_trait_bound < ' a > (
422+ /// Returns `true` if default trait bound should not be added.
423+ fn should_add_default_traits < ' a > (
422424 & self ,
423425 trait_def_id : DefId ,
424426 hir_bounds : & ' a [ hir:: GenericBound < ' tcx > ] ,
425427 self_ty_where_predicates : Option < ( LocalDefId , & ' tcx [ hir:: WherePredicate < ' tcx > ] ) > ,
426428 ) -> bool {
427429 let collected = collect_bounds ( hir_bounds, self_ty_where_predicates, trait_def_id) ;
428- !collected. any ( )
430+ !self . tcx ( ) . has_attr ( CRATE_DEF_ID , sym :: rustc_no_implicit_bounds ) && ! collected. any ( )
429431 }
430432
431433 /// Lower HIR bounds into `bounds` given the self type `param_ty` and the overarching late-bound vars if any.
0 commit comments