Skip to content

Only traverse attrs once while checking for coherence override attributes #93343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions compiler/rustc_trait_selection/src/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::traits::{
self, FulfillmentContext, Normalized, Obligation, ObligationCause, PredicateObligation,
PredicateObligations, SelectionContext,
};
use rustc_ast::Attribute;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_middle::ty::fast_reject::{self, SimplifyParams, StripReferences};
use rustc_middle::ty::fold::TypeFoldable;
Expand Down Expand Up @@ -160,24 +161,30 @@ impl OverlapMode {
}

fn overlap_mode<'tcx>(tcx: TyCtxt<'tcx>, impl1_def_id: DefId, impl2_def_id: DefId) -> OverlapMode {
if tcx.has_attr(impl1_def_id, sym::rustc_strict_coherence)
!= tcx.has_attr(impl2_def_id, sym::rustc_strict_coherence)
{
bug!("Use strict coherence on both impls",);
}

if tcx.has_attr(impl1_def_id, sym::rustc_with_negative_coherence)
!= tcx.has_attr(impl2_def_id, sym::rustc_with_negative_coherence)
{
bug!("Use with negative coherence on both impls",);
}

if tcx.has_attr(impl1_def_id, sym::rustc_strict_coherence) {
OverlapMode::Strict
} else if tcx.has_attr(impl1_def_id, sym::rustc_with_negative_coherence) {
OverlapMode::WithNegative
} else {
OverlapMode::Stable
// Find the possible coherence mode override opt-in attributes for each `DefId`
let find_coherence_attr = |attr: &Attribute| {
let name = attr.name_or_empty();
match name {
sym::rustc_with_negative_coherence | sym::rustc_strict_coherence => Some(name),
_ => None,
}
};
let impl1_coherence_mode = tcx.get_attrs(impl1_def_id).iter().find_map(find_coherence_attr);
let impl2_coherence_mode = tcx.get_attrs(impl2_def_id).iter().find_map(find_coherence_attr);

// If there are any (that currently happens in tests), they need to match. Otherwise, the
// default 1.0 rules are used.
match (impl1_coherence_mode, impl2_coherence_mode) {
(None, None) => OverlapMode::Stable,
(Some(sym::rustc_with_negative_coherence), Some(sym::rustc_with_negative_coherence)) => {
OverlapMode::WithNegative
}
(Some(sym::rustc_strict_coherence), Some(sym::rustc_strict_coherence)) => {
OverlapMode::Strict
}
(Some(mode), _) | (_, Some(mode)) => {
bug!("Use the same coherence mode on both impls: {}", mode)
}
}
}

Expand Down