diff options
| author | Zalathar <Zalathar@users.noreply.github.com> | 2024-02-10 13:54:28 +1100 |
|---|---|---|
| committer | Zalathar <Zalathar@users.noreply.github.com> | 2024-02-10 18:14:02 +1100 |
| commit | 7b73e4fd44c86405307afa5f1a03317bc560f746 (patch) | |
| tree | 9c205b427e1cac811df59c66b06317530297b548 | |
| parent | b5c46dc5426038a49c95398bce30eeb20ec421e2 (diff) | |
| download | rust-7b73e4fd44c86405307afa5f1a03317bc560f746.tar.gz rust-7b73e4fd44c86405307afa5f1a03317bc560f746.zip | |
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.
| -rw-r--r-- | compiler/rustc_hir_analysis/src/coherence/mod.rs | 8 | ||||
| -rw-r--r-- | tests/ui/specialization/min_specialization/allow_internal_unstable.rs (renamed from tests/ui/specialization/allow_internal_unstable.rs) | 9 |
2 files changed, 14 insertions, 3 deletions
diff --git a/compiler/rustc_hir_analysis/src/coherence/mod.rs b/compiler/rustc_hir_analysis/src/coherence/mod.rs index 8cf1f2c9407..817dab993a3 100644 --- a/compiler/rustc_hir_analysis/src/coherence/mod.rs +++ b/compiler/rustc_hir_analysis/src/coherence/mod.rs @@ -10,7 +10,7 @@ use rustc_errors::{codes::*, struct_span_code_err}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::query::Providers; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; -use rustc_span::ErrorGuaranteed; +use rustc_span::{sym, ErrorGuaranteed}; use rustc_trait_selection::traits; mod builtin; @@ -70,7 +70,11 @@ fn enforce_trait_manually_implementable( if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable = tcx.trait_def(trait_def_id).specialization_kind { - if !tcx.features().specialization && !tcx.features().min_specialization { + if !tcx.features().specialization + && !tcx.features().min_specialization + && !impl_header_span.allows_unstable(sym::specialization) + && !impl_header_span.allows_unstable(sym::min_specialization) + { return Err(tcx.dcx().emit_err(errors::SpecializationTrait { span: impl_header_span })); } } diff --git a/tests/ui/specialization/allow_internal_unstable.rs b/tests/ui/specialization/min_specialization/allow_internal_unstable.rs index 317782b7b72..8f3677d9769 100644 --- a/tests/ui/specialization/allow_internal_unstable.rs +++ b/tests/ui/specialization/min_specialization/allow_internal_unstable.rs @@ -5,6 +5,9 @@ #![allow(internal_features)] #![feature(allow_internal_unstable)] +// aux-build:specialization-trait.rs +extern crate specialization_trait; + #[allow_internal_unstable(min_specialization)] macro_rules! test { () => { @@ -12,7 +15,11 @@ macro_rules! test { trait Tr {} impl<U> Tr for T<U> {} impl Tr for T<u8> {} - } + + impl<U> specialization_trait::SpecTrait for T<U> { + fn method(&self) {} + } + }; } test! {} |
