Skip to content

Commit 987872b

Browse files
committed
hir_analysis: add #![rustc_no_implicit_bounds]
Adds a new `rustc_attrs` attribute that stops rustc from adding any default bounds. Useful for tests where default bounds just add noise and make debugging harder.
1 parent 27733d4 commit 987872b

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11081108
TEST, rustc_insignificant_dtor, Normal, template!(Word),
11091109
WarnFollowing, EncodeCrossCrate::Yes
11101110
),
1111+
rustc_attr!(
1112+
TEST, rustc_no_implicit_bounds, CrateLevel, template!(Word),
1113+
WarnFollowing, EncodeCrossCrate::No
1114+
),
11111115
rustc_attr!(
11121116
TEST, rustc_strict_coherence, Normal, template!(Word),
11131117
WarnFollowing, EncodeCrossCrate::Yes

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ use rustc_errors::codes::*;
55
use rustc_errors::struct_span_code_err;
66
use rustc_hir as hir;
77
use rustc_hir::def::{DefKind, Res};
8-
use rustc_hir::def_id::{DefId, LocalDefId};
8+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
99
use rustc_hir::{AmbigArg, LangItem, PolyTraitRef};
1010
use rustc_middle::bug;
1111
use 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};
1616
use rustc_trait_selection::traits;
1717
use smallvec::SmallVec;
1818
use 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.

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,7 @@ symbols! {
18661866
rustc_never_returns_null_ptr,
18671867
rustc_never_type_options,
18681868
rustc_no_implicit_autorefs,
1869+
rustc_no_implicit_bounds,
18691870
rustc_no_mir_inline,
18701871
rustc_nonnull_optimization_guaranteed,
18711872
rustc_nounwind,

0 commit comments

Comments
 (0)