Skip to content

Commit 42a5658

Browse files
committed
Port rustc_strict_coherence to the new attribute parser
1 parent 06cafcb commit 42a5658

7 files changed

Lines changed: 29 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
@@ -810,3 +810,18 @@ impl<S: Stage> SingleAttributeParser<S> for RustcDefPath {
810810
Some(AttributeKind::RustcDefPath(cx.attr_span))
811811
}
812812
}
813+
814+
pub(crate) struct RustcStrictCoherenceParser;
815+
816+
impl<S: Stage> NoArgsAttributeParser<S> for RustcStrictCoherenceParser {
817+
const PATH: &[Symbol] = &[sym::rustc_strict_coherence];
818+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
819+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
820+
Allow(Target::Trait),
821+
Allow(Target::Struct),
822+
Allow(Target::Enum),
823+
Allow(Target::Union),
824+
Allow(Target::ForeignTy),
825+
]);
826+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
827+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ attribute_parsers!(
277277
Single<WithoutArgs<RustcPreserveUbChecksParser>>,
278278
Single<WithoutArgs<RustcReallocatorParser>>,
279279
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
280+
Single<WithoutArgs<RustcStrictCoherenceParser>>,
280281
Single<WithoutArgs<RustcVarianceOfOpaquesParser>>,
281282
Single<WithoutArgs<RustcVarianceParser>>,
282283
Single<WithoutArgs<SpecializationTraitParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,9 @@ pub enum AttributeKind {
12211221
/// Represents `#[rustc_std_internal_symbol]`.
12221222
RustcStdInternalSymbol(Span),
12231223

1224+
/// Represents `#[rustc_strict_coherence]`.
1225+
RustcStrictCoherence(Span),
1226+
12241227
/// Represents `#[rustc_symbol_name]`
12251228
RustcSymbolName(Span),
12261229

@@ -1257,6 +1260,7 @@ pub enum AttributeKind {
12571260
/// Span of the attribute.
12581261
span: Span,
12591262
},
1263+
12601264
/// Represents `#[target_feature(enable = "...")]` and
12611265
/// `#[unsafe(force_target_feature(enable = "...")]`.
12621266
TargetFeature { features: ThinVec<(Symbol, Span)>, attr_span: Span, was_forced: bool },

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl AttributeKind {
150150
RustcSkipDuringMethodDispatch { .. } => No,
151151
RustcSpecializationTrait(..) => No,
152152
RustcStdInternalSymbol(..) => No,
153+
RustcStrictCoherence(..) => Yes,
153154
RustcSymbolName(..) => Yes,
154155
RustcThenThisWouldNeed(..) => No,
155156
RustcUnsafeSpecializationMarker(..) => No,

compiler/rustc_middle/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub(crate) struct StrictCoherenceNeedsNegativeCoherence {
9393
#[primary_span]
9494
pub span: Span,
9595
#[label("due to this attribute")]
96-
pub attr_span: Option<Span>,
96+
pub attr_span: Span,
9797
}
9898

9999
#[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
@@ -341,6 +341,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
341341
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
342342
| AttributeKind::RustcSpecializationTrait(..)
343343
| AttributeKind::RustcStdInternalSymbol (..)
344+
| AttributeKind::RustcStrictCoherence(..)
344345
| AttributeKind::RustcSymbolName(..)
345346
| AttributeKind::RustcThenThisWouldNeed(..)
346347
| AttributeKind::RustcUnsafeSpecializationMarker(..)
@@ -402,7 +403,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
402403
| sym::rustc_autodiff
403404
| sym::rustc_capture_analysis
404405
| sym::rustc_regions
405-
| sym::rustc_strict_coherence
406406
| sym::rustc_mir
407407
| sym::rustc_outlives
408408
| sym::rustc_evaluate_where_clauses

0 commit comments

Comments
 (0)