Skip to content

Commit 12f0ef7

Browse files
authored
Rollup merge of #152171 - jdonszelmann:port-rustc-strict-coherence, r=jonathanbrouwer
Port `rustc_strict_coherence` to the new attribute parser r? @JonathanBrouwer
2 parents 11c7917 + 2dda303 commit 12f0ef7

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
@@ -845,3 +845,18 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicConstStableIndirectPar
845845
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
846846
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsicConstStableIndirect;
847847
}
848+
849+
pub(crate) struct RustcStrictCoherenceParser;
850+
851+
impl<S: Stage> NoArgsAttributeParser<S> for RustcStrictCoherenceParser {
852+
const PATH: &[Symbol] = &[sym::rustc_strict_coherence];
853+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
854+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
855+
Allow(Target::Trait),
856+
Allow(Target::Struct),
857+
Allow(Target::Enum),
858+
Allow(Target::Union),
859+
Allow(Target::ForeignTy),
860+
]);
861+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
862+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ attribute_parsers!(
289289
Single<WithoutArgs<RustcReallocatorParser>>,
290290
Single<WithoutArgs<RustcRegionsParser>>,
291291
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
292+
Single<WithoutArgs<RustcStrictCoherenceParser>>,
292293
Single<WithoutArgs<RustcVarianceOfOpaquesParser>>,
293294
Single<WithoutArgs<RustcVarianceParser>>,
294295
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
@@ -1239,6 +1239,9 @@ pub enum AttributeKind {
12391239
/// Represents `#[rustc_std_internal_symbol]`.
12401240
RustcStdInternalSymbol(Span),
12411241

1242+
/// Represents `#[rustc_strict_coherence]`.
1243+
RustcStrictCoherence(Span),
1244+
12421245
/// Represents `#[rustc_symbol_name]`
12431246
RustcSymbolName(Span),
12441247

@@ -1275,6 +1278,7 @@ pub enum AttributeKind {
12751278
/// Span of the attribute.
12761279
span: Span,
12771280
},
1281+
12781282
/// Represents `#[target_feature(enable = "...")]` and
12791283
/// `#[unsafe(force_target_feature(enable = "...")]`.
12801284
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
@@ -156,6 +156,7 @@ impl AttributeKind {
156156
RustcSkipDuringMethodDispatch { .. } => No,
157157
RustcSpecializationTrait(..) => No,
158158
RustcStdInternalSymbol(..) => No,
159+
RustcStrictCoherence(..) => Yes,
159160
RustcSymbolName(..) => Yes,
160161
RustcThenThisWouldNeed(..) => No,
161162
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
@@ -347,6 +347,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
347347
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
348348
| AttributeKind::RustcSpecializationTrait(..)
349349
| AttributeKind::RustcStdInternalSymbol (..)
350+
| AttributeKind::RustcStrictCoherence(..)
350351
| AttributeKind::RustcSymbolName(..)
351352
| AttributeKind::RustcThenThisWouldNeed(..)
352353
| AttributeKind::RustcUnsafeSpecializationMarker(..)
@@ -405,7 +406,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
405406
| sym::rustc_never_type_options
406407
| sym::rustc_autodiff
407408
| sym::rustc_capture_analysis
408-
| sym::rustc_strict_coherence
409409
| sym::rustc_mir
410410
| sym::rustc_partition_reused
411411
| sym::rustc_partition_codegened

0 commit comments

Comments
 (0)