Skip to content

Commit

Permalink
Allow restricted trait impls in macros with min_specialization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Zalathar committed Feb 10, 2024
1 parent b5c46dc commit 7b73e4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_hir_analysis/src/coherence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 }));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
#![allow(internal_features)]
#![feature(allow_internal_unstable)]

// aux-build:specialization-trait.rs
extern crate specialization_trait;

#[allow_internal_unstable(min_specialization)]
macro_rules! test {
() => {
struct T<U>(U);
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! {}

0 comments on commit 7b73e4f

Please sign in to comment.