Skip to content

Commit 0891cb4

Browse files
authored
Rollup merge of rust-lang#119963 - clubby789:spec-allow-internal-unstable, r=compiler-errors
Fix `allow_internal_unstable` for `(min_)specialization` Fixes rust-lang#119950 Blocked on rust-lang#119949 (comment doesn't make sense until that merges) I'd like to follow this up and look for more instances of not properly checking spans for features but I wanted to fix the motivating issue.
2 parents 1e46be6 + eb63d3a commit 0891cb4

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

compiler/rustc_index_macros/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ mod newtype;
3939
feature = "nightly",
4040
allow_internal_unstable(step_trait, rustc_attrs, trusted_step, spec_option_partial_eq)
4141
)]
42+
// FIXME: Remove the above comment about `min_specialization` once bootstrap is bumped,
43+
// and the corresponding one on SpecOptionPartialEq
44+
#[cfg_attr(all(feature = "nightly", not(bootstrap)), allow_internal_unstable(min_specialization))]
4245
pub fn newtype_index(input: TokenStream) -> TokenStream {
4346
newtype::newtype(input)
4447
}

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt, TypeVisitableExt};
2626
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
2727
use rustc_session::lint::builtin::COHERENCE_LEAK_CHECK;
2828
use rustc_session::lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS;
29-
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
29+
use rustc_span::{sym, ErrorGuaranteed, Span, DUMMY_SP};
3030

3131
use super::util;
3232
use super::SelectionContext;
@@ -142,10 +142,30 @@ pub fn translate_args_with_cause<'tcx>(
142142
pub(super) fn specializes(tcx: TyCtxt<'_>, (impl1_def_id, impl2_def_id): (DefId, DefId)) -> bool {
143143
// The feature gate should prevent introducing new specializations, but not
144144
// taking advantage of upstream ones.
145+
// If specialization is enabled for this crate then no extra checks are needed.
146+
// If it's not, and either of the `impl`s is local to this crate, then this definitely
147+
// isn't specializing - unless specialization is enabled for the `impl` span,
148+
// e.g. if it comes from an `allow_internal_unstable` macro
145149
let features = tcx.features();
146150
let specialization_enabled = features.specialization || features.min_specialization;
147-
if !specialization_enabled && (impl1_def_id.is_local() || impl2_def_id.is_local()) {
148-
return false;
151+
if !specialization_enabled {
152+
if impl1_def_id.is_local() {
153+
let span = tcx.def_span(impl1_def_id);
154+
if !span.allows_unstable(sym::specialization)
155+
&& !span.allows_unstable(sym::min_specialization)
156+
{
157+
return false;
158+
}
159+
}
160+
161+
if impl2_def_id.is_local() {
162+
let span = tcx.def_span(impl2_def_id);
163+
if !span.allows_unstable(sym::specialization)
164+
&& !span.allows_unstable(sym::min_specialization)
165+
{
166+
return false;
167+
}
168+
}
149169
}
150170

151171
// We determine whether there's a subset relationship by:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
// test for #119950
3+
// compile-flags: --crate-type lib
4+
5+
#![allow(internal_features)]
6+
#![feature(allow_internal_unstable)]
7+
8+
#[allow_internal_unstable(min_specialization)]
9+
macro_rules! test {
10+
() => {
11+
struct T<U>(U);
12+
trait Tr {}
13+
impl<U> Tr for T<U> {}
14+
impl Tr for T<u8> {}
15+
}
16+
}
17+
18+
test! {}

0 commit comments

Comments
 (0)