Skip to content

Commit 717f1f5

Browse files
committed
Port rustc_strict_coherence to the new attribute parser
1 parent 4935b73 commit 717f1f5

7 files changed

Lines changed: 28 additions & 15 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,3 +778,18 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcEffectiveVisibilityParser {
778778
]);
779779
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEffectiveVisibility;
780780
}
781+
782+
pub(crate) struct RustcStrictCoherenceParser;
783+
784+
impl<S: Stage> NoArgsAttributeParser<S> for RustcStrictCoherenceParser {
785+
const PATH: &'static [Symbol] = &[sym::rustc_strict_coherence];
786+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
787+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
788+
Allow(Target::Trait),
789+
Allow(Target::Struct),
790+
Allow(Target::Enum),
791+
Allow(Target::Union),
792+
Allow(Target::ForeignTy),
793+
]);
794+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
795+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ attribute_parsers!(
274274
Single<WithoutArgs<RustcPreserveUbChecksParser>>,
275275
Single<WithoutArgs<RustcReallocatorParser>>,
276276
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
277+
Single<WithoutArgs<RustcStrictCoherenceParser>>,
277278
Single<WithoutArgs<RustcVarianceOfOpaquesParser>>,
278279
Single<WithoutArgs<RustcVarianceParser>>,
279280
Single<WithoutArgs<SpecializationTraitParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,9 @@ pub enum AttributeKind {
12071207
/// Represents `#[rustc_std_internal_symbol]`.
12081208
RustcStdInternalSymbol(Span),
12091209

1210+
/// Represents `#[rustc_strict_coherence]`.
1211+
RustcStrictCoherence(Span),
1212+
12101213
/// Represents `#[rustc_then_this_would_need]`
12111214
RustcThenThisWouldNeed(Span, ThinVec<Ident>),
12121215

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ impl AttributeKind {
148148
RustcSkipDuringMethodDispatch { .. } => No,
149149
RustcSpecializationTrait(..) => No,
150150
RustcStdInternalSymbol(..) => No,
151+
RustcStrictCoherence(..) => Yes,
151152
RustcThenThisWouldNeed(..) => No,
152153
RustcUnsafeSpecializationMarker(..) => No,
153154
RustcVariance => No,

compiler/rustc_middle/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(crate) struct StrictCoherenceNeedsNegativeCoherence {
8989
#[primary_span]
9090
pub span: Span,
9191
#[label]
92-
pub attr_span: Option<Span>,
92+
pub attr_span: Span,
9393
}
9494

9595
#[derive(Diagnostic)]

compiler/rustc_middle/src/traits/specialization_graph.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustc_data_structures::fx::FxIndexMap;
22
use rustc_errors::ErrorGuaranteed;
3+
use rustc_hir::attrs::AttributeKind;
34
use rustc_hir::def_id::{DefId, DefIdMap};
5+
use rustc_hir::find_attr;
46
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
5-
use rustc_span::sym;
67

78
use crate::error::StrictCoherenceNeedsNegativeCoherence;
89
use crate::ty::fast_reject::SimplifiedType;
@@ -61,23 +62,15 @@ pub enum OverlapMode {
6162
impl OverlapMode {
6263
pub fn get(tcx: TyCtxt<'_>, trait_id: DefId) -> OverlapMode {
6364
let with_negative_coherence = tcx.features().with_negative_coherence();
64-
let strict_coherence = tcx.has_attr(trait_id, sym::rustc_strict_coherence);
65+
let strict_coherence = find_attr!(tcx.get_all_attrs(trait_id), AttributeKind::RustcStrictCoherence(span) => *span);
6566

6667
if with_negative_coherence {
67-
if strict_coherence { OverlapMode::Strict } else { OverlapMode::WithNegative }
68+
if strict_coherence.is_some() { OverlapMode::Strict } else { OverlapMode::WithNegative }
6869
} else {
69-
if strict_coherence {
70-
let attr_span = trait_id
71-
.as_local()
72-
.into_iter()
73-
.flat_map(|local_def_id| {
74-
tcx.hir_attrs(tcx.local_def_id_to_hir_id(local_def_id))
75-
})
76-
.find(|attr| attr.has_name(sym::rustc_strict_coherence))
77-
.map(|attr| attr.span());
70+
if let Some(span) = strict_coherence {
7871
tcx.dcx().emit_err(StrictCoherenceNeedsNegativeCoherence {
7972
span: tcx.def_span(trait_id),
80-
attr_span,
73+
attr_span: span,
8174
});
8275
}
8376
OverlapMode::Stable

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
339339
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
340340
| AttributeKind::RustcSpecializationTrait(..)
341341
| AttributeKind::RustcStdInternalSymbol (..)
342+
| AttributeKind::RustcStrictCoherence(..)
342343
| AttributeKind::RustcThenThisWouldNeed(..)
343344
| AttributeKind::RustcUnsafeSpecializationMarker(..)
344345
| AttributeKind::RustcVariance
@@ -400,7 +401,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
400401
| sym::rustc_autodiff
401402
| sym::rustc_capture_analysis
402403
| sym::rustc_regions
403-
| sym::rustc_strict_coherence
404404
| sym::rustc_mir
405405
| sym::rustc_outlives
406406
| sym::rustc_symbol_name

0 commit comments

Comments
 (0)