Skip to content

Commit e51f69e

Browse files
committed
Port rustc_strict_coherence to the new attribute parser
1 parent 56aaf58 commit e51f69e

6 files changed

Lines changed: 27 additions & 14 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
@@ -760,3 +760,18 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcEffectiveVisibilityParser {
760760
]);
761761
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEffectiveVisibility;
762762
}
763+
764+
pub(crate) struct RustcStrictCoherenceParser;
765+
766+
impl<S: Stage> NoArgsAttributeParser<S> for RustcStrictCoherenceParser {
767+
const PATH: &'static [Symbol] = &[sym::rustc_strict_coherence];
768+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
769+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
770+
Allow(Target::Trait),
771+
Allow(Target::Struct),
772+
Allow(Target::Enum),
773+
Allow(Target::Union),
774+
Allow(Target::ForeignTy),
775+
]);
776+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
777+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ attribute_parsers!(
275275
Single<WithoutArgs<RustcPreserveUbChecksParser>>,
276276
Single<WithoutArgs<RustcReallocatorParser>>,
277277
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
278+
Single<WithoutArgs<RustcStrictCoherenceParser>>,
278279
Single<WithoutArgs<RustcVarianceOfOpaquesParser>>,
279280
Single<WithoutArgs<RustcVarianceParser>>,
280281
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
@@ -1218,6 +1218,9 @@ pub enum AttributeKind {
12181218
/// Represents `#[rustc_std_internal_symbol]`.
12191219
RustcStdInternalSymbol(Span),
12201220

1221+
/// Represents `#[rustc_strict_coherence]`.
1222+
RustcStrictCoherence(Span),
1223+
12211224
/// Represents `#[rustc_then_this_would_need]`
12221225
RustcThenThisWouldNeed(Span, ThinVec<Ident>),
12231226

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

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

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
@@ -340,6 +340,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
340340
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
341341
| AttributeKind::RustcSpecializationTrait(..)
342342
| AttributeKind::RustcStdInternalSymbol (..)
343+
| AttributeKind::RustcStrictCoherence(..)
343344
| AttributeKind::RustcThenThisWouldNeed(..)
344345
| AttributeKind::RustcUnsafeSpecializationMarker(..)
345346
| 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)