Skip to content

Commit 7b73e4f

Browse files
committed
Allow restricted trait impls in macros with min_specialization
Implementing traits marked with `#[rustc_specialization_trait]` normally requires (min-)specialization to be enabled for the enclosing crate. With this change, that permission can also be granted by an `allow_internal_unstable` attribute on the macro that generates the impl.
1 parent b5c46dc commit 7b73e4f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

compiler/rustc_hir_analysis/src/coherence/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_errors::{codes::*, struct_span_code_err};
1010
use rustc_hir::def_id::{DefId, LocalDefId};
1111
use rustc_middle::query::Providers;
1212
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
13-
use rustc_span::ErrorGuaranteed;
13+
use rustc_span::{sym, ErrorGuaranteed};
1414
use rustc_trait_selection::traits;
1515

1616
mod builtin;
@@ -70,7 +70,11 @@ fn enforce_trait_manually_implementable(
7070
if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable =
7171
tcx.trait_def(trait_def_id).specialization_kind
7272
{
73-
if !tcx.features().specialization && !tcx.features().min_specialization {
73+
if !tcx.features().specialization
74+
&& !tcx.features().min_specialization
75+
&& !impl_header_span.allows_unstable(sym::specialization)
76+
&& !impl_header_span.allows_unstable(sym::min_specialization)
77+
{
7478
return Err(tcx.dcx().emit_err(errors::SpecializationTrait { span: impl_header_span }));
7579
}
7680
}

tests/ui/specialization/allow_internal_unstable.rs renamed to tests/ui/specialization/min_specialization/allow_internal_unstable.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
#![allow(internal_features)]
66
#![feature(allow_internal_unstable)]
77

8+
// aux-build:specialization-trait.rs
9+
extern crate specialization_trait;
10+
811
#[allow_internal_unstable(min_specialization)]
912
macro_rules! test {
1013
() => {
1114
struct T<U>(U);
1215
trait Tr {}
1316
impl<U> Tr for T<U> {}
1417
impl Tr for T<u8> {}
15-
}
18+
19+
impl<U> specialization_trait::SpecTrait for T<U> {
20+
fn method(&self) {}
21+
}
22+
};
1623
}
1724

1825
test! {}

0 commit comments

Comments
 (0)