Skip to content

Commit 5bf9d61

Browse files
author
Yuki Okushi
authored
Rollup merge of rust-lang#103772 - compiler-errors:better-strict-coherence-err, r=davidtwco
better error for `rustc_strict_coherence` misuse Fixes rust-lang#103753
2 parents 2a6a8f4 + 953727f commit 5bf9d61

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

compiler/rustc_error_messages/locales/en-US/middle.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ middle_values_too_big =
2727
2828
middle_cannot_be_normalized =
2929
unable to determine layout for `{$ty}` because `{$failure_ty}` cannot be normalized
30+
31+
middle_strict_coherence_needs_negative_coherence =
32+
to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled
33+
.label = due to this attribute

compiler/rustc_middle/src/error.rs

+9
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,12 @@ pub struct ConstEvalNonIntError {
5555
#[primary_span]
5656
pub span: Span,
5757
}
58+
59+
#[derive(Diagnostic)]
60+
#[diag(middle_strict_coherence_needs_negative_coherence)]
61+
pub(crate) struct StrictCoherenceNeedsNegativeCoherence {
62+
#[primary_span]
63+
pub span: Span,
64+
#[label]
65+
pub attr_span: Option<Span>,
66+
}

compiler/rustc_middle/src/traits/specialization_graph.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::StrictCoherenceNeedsNegativeCoherence;
12
use crate::ty::fast_reject::SimplifiedType;
23
use crate::ty::visit::TypeVisitable;
34
use crate::ty::{self, TyCtxt};
@@ -65,9 +66,21 @@ impl OverlapMode {
6566

6667
if with_negative_coherence {
6768
if strict_coherence { OverlapMode::Strict } else { OverlapMode::WithNegative }
68-
} else if strict_coherence {
69-
bug!("To use strict_coherence you need to set with_negative_coherence feature flag");
7069
} else {
70+
if strict_coherence {
71+
let attr_span = trait_id
72+
.as_local()
73+
.into_iter()
74+
.flat_map(|local_def_id| {
75+
tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(local_def_id))
76+
})
77+
.find(|attr| attr.has_name(sym::rustc_strict_coherence))
78+
.map(|attr| attr.span);
79+
tcx.sess.emit_err(StrictCoherenceNeedsNegativeCoherence {
80+
span: tcx.def_span(trait_id),
81+
attr_span,
82+
});
83+
}
7184
OverlapMode::Stable
7285
}
7386
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[rustc_strict_coherence]
4+
trait Foo {}
5+
//~^ ERROR to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled
2+
--> $DIR/strict-coherence-needs-negative-coherence.rs:4:1
3+
|
4+
LL | #[rustc_strict_coherence]
5+
| ------------------------- due to this attribute
6+
LL | trait Foo {}
7+
| ^^^^^^^^^
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)