Rollup merge of #120870 - Zalathar:allow-min-spec, r=oli-obk

Allow restricted trait impls under `#[allow_internal_unstable(min_specialization)]`

This is a follow-up to #119963 and a companion to #120866, though it can land independently from the latter.

---

We have several compiler crates that only enable `#[feature(min_specialization)]` because it is required by their expansions of `newtype_index!`, in order to implement traits marked with `#[rustc_specialization_trait]`.

This PR allows those traits to be implemented internally by macros with `#[allow_internal_unstable(min_specialization)]`, without needing specialization to be enabled in the enclosing crate.
This commit is contained in:
Matthias Krüger 2024-02-10 13:12:31 +01:00 committed by GitHub
commit 55913368c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 25 additions and 14 deletions

View File

@ -15,7 +15,7 @@
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(negative_impls)]
#![feature(stmt_expr_attributes)]

View File

@ -7,6 +7,7 @@
#![allow(internal_features)]
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![doc(rust_logo)]
#![feature(array_windows)]
@ -16,7 +17,6 @@
#![feature(error_reporter)]
#![feature(extract_if)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(rustc_attrs)]

View File

@ -5,7 +5,7 @@
#![feature(associated_type_defaults)]
#![feature(closure_track_caller)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(never_type)]
#![feature(rustc_attrs)]
#![feature(variant_count)]

View File

@ -10,7 +10,7 @@
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 }));
}
}

View File

@ -67,7 +67,7 @@
#![feature(is_sorted)]
#![feature(iter_intersperse)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(never_type)]
#![feature(lazy_cell)]
#![feature(slice_partition_dedup)]

View File

@ -5,7 +5,7 @@
#![feature(try_blocks)]
#![feature(never_type)]
#![feature(box_patterns)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(control_flow_enum)]
#[macro_use]

View File

@ -24,7 +24,7 @@
#![feature(let_chains)]
#![feature(if_let_guard)]
#![feature(iterator_try_collect)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(try_blocks)]
#![recursion_limit = "512"] // For rustdoc

View File

@ -9,7 +9,7 @@
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(try_blocks)]
#[macro_use]

View File

@ -2,7 +2,7 @@
#![feature(box_patterns)]
#![feature(exact_size_is_empty)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(try_blocks)]
#[macro_use]

View File

@ -8,7 +8,7 @@
#![feature(is_sorted)]
#![feature(let_chains)]
#![feature(map_try_insert)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(never_type)]
#![feature(option_get_or_insert_default)]
#![feature(round_char_boundary)]

View File

@ -14,7 +14,7 @@
#![feature(exhaustive_patterns)]
#![feature(iter_intersperse)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![feature(rustc_attrs)]
#![feature(step_trait)]
#![allow(internal_features)]

View File

@ -24,7 +24,7 @@
#![feature(option_take_if)]
#![feature(never_type)]
#![feature(type_alias_impl_trait)]
#![feature(min_specialization)]
#![cfg_attr(bootstrap, feature(min_specialization))]
#![recursion_limit = "512"] // For rustdoc
#[macro_use]

View File

@ -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! {}