Implement soundness check for min_specialization

This commit is contained in:
Matthew Jasper 2020-02-08 20:14:02 +00:00
parent 32d330df30
commit 0bbbe719e8
24 changed files with 796 additions and 5 deletions

View File

@ -79,10 +79,18 @@ impl<'tcx> TypeVisitor<'tcx> for ParameterCollector {
}
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
if let ty::ConstKind::Param(data) = c.val {
self.parameters.push(Parameter::from(data));
match c.val {
ty::ConstKind::Unevaluated(..) if !self.include_nonconstraining => {
// Constant expressions are not injective
return c.ty.visit_with(self);
}
ty::ConstKind::Param(data) => {
self.parameters.push(Parameter::from(data));
}
_ => {}
}
false
c.super_visit_with(self)
}
}

View File

@ -9,6 +9,8 @@
//! fixed, but for the moment it's easier to do these checks early.
use crate::constrained_generic_params as cgp;
use min_specialization::check_min_specialization;
use rustc::ty::query::Providers;
use rustc::ty::{self, TyCtxt, TypeFoldable};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@ -16,9 +18,11 @@ use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_span::Span;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use rustc_span::Span;
mod min_specialization;
/// Checks that all the type/lifetime parameters on an impl also
/// appear in the trait ref or self type (or are constrained by a
@ -60,7 +64,9 @@ pub fn impl_wf_check(tcx: TyCtxt<'_>) {
}
fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: DefId) {
tcx.hir().visit_item_likes_in_module(module_def_id, &mut ImplWfCheck { tcx });
let min_specialization = tcx.features().min_specialization;
tcx.hir()
.visit_item_likes_in_module(module_def_id, &mut ImplWfCheck { tcx, min_specialization });
}
pub fn provide(providers: &mut Providers<'_>) {
@ -69,6 +75,7 @@ pub fn provide(providers: &mut Providers<'_>) {
struct ImplWfCheck<'tcx> {
tcx: TyCtxt<'tcx>,
min_specialization: bool,
}
impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> {
@ -77,6 +84,9 @@ impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> {
let impl_def_id = self.tcx.hir().local_def_id(item.hir_id);
enforce_impl_params_are_constrained(self.tcx, impl_def_id, items);
enforce_impl_items_are_distinct(self.tcx, items);
if self.min_specialization {
check_min_specialization(self.tcx, impl_def_id, item.span);
}
}
}

View File

@ -0,0 +1,390 @@
//! # Minimal Specialization
//!
//! This module contains the checks for sound specialization used when the
//! `min_specialization` feature is enabled. This requires that the impl is
//! *always applicable*.
//!
//! If `impl1` specializes `impl2` then `impl1` is always applicable if we know
//! that all the bounds of `impl2` are satisfied, and all of the bounds of
//! `impl1` are satisfied for some choice of lifetimes then we know that
//! `impl1` applies for any choice of lifetimes.
//!
//! ## Basic approach
//!
//! To enforce this requirement on specializations we take the following
//! approach:
//!
//! 1. Match up the substs for `impl2` so that the implemented trait and
//! self-type match those for `impl1`.
//! 2. Check for any direct use of `'static` in the substs of `impl2`.
//! 3. Check that all of the generic parameters of `impl1` occur at most once
//! in the *unconstrained* substs for `impl2`. A parameter is constrained if
//! its value is completely determined by an associated type projection
//! predicate.
//! 4. Check that all predicates on `impl1` also exist on `impl2` (after
//! matching substs).
//!
//! ## Example
//!
//! Suppose we have the following always applicable impl:
//!
//! ```rust
//! impl<T> SpecExtend<T> for std::vec::IntoIter<T> { /* specialized impl */ }
//! impl<T, I: Iterator<Item=T>> SpecExtend<T> for I { /* default impl */ }
//! ```
//!
//! We get that the subst for `impl2` are `[T, std::vec::IntoIter<T>]`. `T` is
//! constrained to be `<I as Iterator>::Item`, so we check only
//! `std::vec::IntoIter<T>` for repeated parameters, which it doesn't have. The
//! predicates of `impl1` are only `T: Sized`, which is also a predicate of
//! `impl2`. So this specialization is sound.
//!
//! ## Extensions
//!
//! Unfortunately not all specializations in the standard library are allowed
//! by this. So there are two extensions to these rules that allow specializing
//! on some traits: that is, using them as bounds on the specializing impl,
//! even when they don't occur in the base impl.
//!
//! ### rustc_specialization_trait
//!
//! If a trait is always applicable, then it's sound to specialize on it. We
//! check trait is always applicable in the same way as impls, except that step
//! 4 is now "all predicates on `impl1` are always applicable". We require that
//! `specialization` or `min_specialization` is enabled to implement these
//! traits.
//!
//! ### rustc_unsafe_specialization_marker
//!
//! There are also some specialization on traits with no methods, including the
//! stable `FusedIterator` trait. We allow marking marker traits with an
//! unstable attribute that means we ignore them in point 3 of the checks
//! above. This is unsound, in the sense that the specialized impl may be used
//! when it doesn't apply, but we allow it in the short term since it can't
//! cause use after frees with purely safe code in the same way as specializing
//! on traits with methods can.
use crate::constrained_generic_params as cgp;
use rustc::middle::region::ScopeTree;
use rustc::ty::subst::{GenericArg, InternalSubsts, SubstsRef};
use rustc::ty::trait_def::TraitSpecializationKind;
use rustc::ty::{self, InstantiatedPredicates, TyCtxt, TypeFoldable};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{InferCtxt, SuppressRegionErrors, TyCtxtInferExt};
use rustc_infer::traits::specialization_graph::Node;
use rustc_span::Span;
use rustc_trait_selection::traits::{self, translate_substs, wf};
pub(super) fn check_min_specialization(tcx: TyCtxt<'_>, impl_def_id: DefId, span: Span) {
if let Some(node) = parent_specialization_node(tcx, impl_def_id) {
tcx.infer_ctxt().enter(|infcx| {
check_always_applicable(&infcx, impl_def_id, node, span);
});
}
}
fn parent_specialization_node(tcx: TyCtxt<'_>, impl1_def_id: DefId) -> Option<Node> {
let trait_ref = tcx.impl_trait_ref(impl1_def_id)?;
let trait_def = tcx.trait_def(trait_ref.def_id);
let impl2_node = trait_def.ancestors(tcx, impl1_def_id).ok()?.nth(1)?;
let always_applicable_trait =
matches!(trait_def.specialization_kind, TraitSpecializationKind::AlwaysApplicable);
if impl2_node.is_from_trait() && !always_applicable_trait {
// Implementing a normal trait isn't a specialization.
return None;
}
Some(impl2_node)
}
/// Check that `impl1` is a sound specialization
fn check_always_applicable(
infcx: &InferCtxt<'_, '_>,
impl1_def_id: DefId,
impl2_node: Node,
span: Span,
) {
if let Some((impl1_substs, impl2_substs)) =
get_impl_substs(infcx, impl1_def_id, impl2_node, span)
{
let impl2_def_id = impl2_node.def_id();
debug!(
"check_always_applicable(\nimpl1_def_id={:?},\nimpl2_def_id={:?},\nimpl2_substs={:?}\n)",
impl1_def_id, impl2_def_id, impl2_substs
);
let tcx = infcx.tcx;
let parent_substs = if impl2_node.is_from_trait() {
impl2_substs.to_vec()
} else {
unconstrained_parent_impl_substs(tcx, impl2_def_id, impl2_substs)
};
check_static_lifetimes(tcx, &parent_substs, span);
check_duplicate_params(tcx, impl1_substs, &parent_substs, span);
check_predicates(tcx, impl1_def_id, impl1_substs, impl2_node, impl2_substs, span);
}
}
/// Given a specializing impl `impl1`, and the base impl `impl2`, returns two
/// substitutions `(S1, S2)` that equate their trait references. The returned
/// types are expressed in terms of the generics of `impl1`.
///
/// Example
///
/// impl<A, B> Foo<A> for B { /* impl2 */ }
/// impl<C> Foo<Vec<C>> for C { /* impl1 */ }
///
/// Would return `S1 = [C]` and `S2 = [Vec<C>, C]`.
fn get_impl_substs<'tcx>(
infcx: &InferCtxt<'_, 'tcx>,
impl1_def_id: DefId,
impl2_node: Node,
span: Span,
) -> Option<(SubstsRef<'tcx>, SubstsRef<'tcx>)> {
let tcx = infcx.tcx;
let param_env = tcx.param_env(impl1_def_id);
let impl1_substs = InternalSubsts::identity_for_item(tcx, impl1_def_id);
let impl2_substs = translate_substs(infcx, param_env, impl1_def_id, impl1_substs, impl2_node);
// Conservatively use an empty `ParamEnv`.
let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty());
infcx.resolve_regions_and_report_errors(
impl1_def_id,
&ScopeTree::default(),
&outlives_env,
SuppressRegionErrors::default(),
);
let impl2_substs = match infcx.fully_resolve(&impl2_substs) {
Ok(s) => s,
Err(_) => {
tcx.sess.struct_span_err(span, "could not resolve substs on overridden impl").emit();
return None;
}
};
Some((impl1_substs, impl2_substs))
}
/// Returns a list of all of the unconstrained subst of the given impl.
///
/// For example given the impl:
///
/// impl<'a, T, I> ... where &'a I: IntoIterator<Item=&'a T>
///
/// This would return the substs corresponding to `['a, I]`, because knowing
/// `'a` and `I` determines the value of `T`.
fn unconstrained_parent_impl_substs<'tcx>(
tcx: TyCtxt<'tcx>,
impl_def_id: DefId,
impl_substs: SubstsRef<'tcx>,
) -> Vec<GenericArg<'tcx>> {
let impl_generic_predicates = tcx.predicates_of(impl_def_id);
let mut unconstrained_parameters = FxHashSet::default();
let mut constrained_params = FxHashSet::default();
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id);
// Unfortunately the functions in `constrained_generic_parameters` don't do
// what we want here. We want only a list of constrained parameters while
// the functions in `cgp` add the constrained parameters to a list of
// unconstrained parameters.
for (predicate, _) in impl_generic_predicates.predicates.iter() {
if let ty::Predicate::Projection(proj) = predicate {
let projection_ty = proj.skip_binder().projection_ty;
let projected_ty = proj.skip_binder().ty;
let unbound_trait_ref = projection_ty.trait_ref(tcx);
if Some(unbound_trait_ref) == impl_trait_ref {
continue;
}
unconstrained_parameters.extend(cgp::parameters_for(&projection_ty, true));
for param in cgp::parameters_for(&projected_ty, false) {
if !unconstrained_parameters.contains(&param) {
constrained_params.insert(param.0);
}
}
unconstrained_parameters.extend(cgp::parameters_for(&projected_ty, true));
}
}
impl_substs
.iter()
.enumerate()
.filter(|&(idx, _)| !constrained_params.contains(&(idx as u32)))
.map(|(_, arg)| *arg)
.collect()
}
/// Check that parameters of the derived impl don't occur more than once in the
/// equated substs of the base impl.
///
/// For example forbid the following:
///
/// impl<A> Tr for A { }
/// impl<B> Tr for (B, B) { }
///
/// Note that only consider the unconstrained parameters of the base impl:
///
/// impl<S, I: IntoIterator<Item = S>> Tr<S> for I { }
/// impl<T> Tr<T> for Vec<T> { }
///
/// The substs for the parent impl here are `[T, Vec<T>]`, which repeats `T`,
/// but `S` is constrained in the parent impl, so `parent_substs` is only
/// `[Vec<T>]`. This means we allow this impl.
fn check_duplicate_params<'tcx>(
tcx: TyCtxt<'tcx>,
impl1_substs: SubstsRef<'tcx>,
parent_substs: &Vec<GenericArg<'tcx>>,
span: Span,
) {
let mut base_params = cgp::parameters_for(parent_substs, true);
base_params.sort_by_key(|param| param.0);
if let (_, [duplicate, ..]) = base_params.partition_dedup() {
let param = impl1_substs[duplicate.0 as usize];
tcx.sess
.struct_span_err(span, &format!("specializing impl repeats parameter `{}`", param))
.emit();
}
}
/// Check that `'static` lifetimes are not introduced by the specializing impl.
///
/// For example forbid the following:
///
/// impl<A> Tr for A { }
/// impl Tr for &'static i32 { }
fn check_static_lifetimes<'tcx>(
tcx: TyCtxt<'tcx>,
parent_substs: &Vec<GenericArg<'tcx>>,
span: Span,
) {
if tcx.any_free_region_meets(parent_substs, |r| *r == ty::ReStatic) {
tcx.sess.struct_span_err(span, &format!("cannot specialize on `'static` lifetime")).emit();
}
}
/// Check whether predicates on the specializing impl (`impl1`) are allowed.
///
/// Each predicate `P` must be:
///
/// * global (not reference any parameters)
/// * `T: Tr` predicate where `Tr` is an always-applicable trait
/// * on the base `impl impl2`
/// * Currently this check is done using syntactic equality, which is
/// conservative but generally sufficient.
fn check_predicates<'tcx>(
tcx: TyCtxt<'tcx>,
impl1_def_id: DefId,
impl1_substs: SubstsRef<'tcx>,
impl2_node: Node,
impl2_substs: SubstsRef<'tcx>,
span: Span,
) {
let impl1_predicates = tcx.predicates_of(impl1_def_id).instantiate(tcx, impl1_substs);
let mut impl2_predicates = if impl2_node.is_from_trait() {
// Always applicable traits have to be always applicable without any
// assumptions.
InstantiatedPredicates::empty()
} else {
tcx.predicates_of(impl2_node.def_id()).instantiate(tcx, impl2_substs)
};
debug!(
"check_always_applicable(\nimpl1_predicates={:?},\nimpl2_predicates={:?}\n)",
impl1_predicates, impl2_predicates,
);
// Since impls of always applicable traits don't get to assume anything, we
// can also assume their supertraits apply.
//
// For example, we allow:
//
// #[rustc_specialization_trait]
// trait AlwaysApplicable: Debug { }
//
// impl<T> Tr for T { }
// impl<T: AlwaysApplicable> Tr for T { }
//
// Specializing on `AlwaysApplicable` allows also specializing on `Debug`
// which is sound because we forbid impls like the following
//
// impl<D: Debug> AlwaysApplicable for D { }
let always_applicable_traits: Vec<_> = impl1_predicates
.predicates
.iter()
.filter(|predicate| {
matches!(
trait_predicate_kind(tcx, predicate),
Some(TraitSpecializationKind::AlwaysApplicable)
)
})
.copied()
.collect();
impl2_predicates.predicates.extend(traits::elaborate_predicates(tcx, always_applicable_traits));
for predicate in impl1_predicates.predicates {
if !impl2_predicates.predicates.contains(&predicate) {
check_specialization_on(tcx, &predicate, span)
}
}
}
fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: &ty::Predicate<'tcx>, span: Span) {
debug!("can_specialize_on(predicate = {:?})", predicate);
match predicate {
// Global predicates are either always true or always false, so we
// are fine to specialize on.
_ if predicate.is_global() => (),
// We allow specializing on explicitly marked traits with no associated
// items.
ty::Predicate::Trait(pred, hir::Constness::NotConst) => {
if !matches!(
trait_predicate_kind(tcx, predicate),
Some(TraitSpecializationKind::Marker)
) {
tcx.sess
.struct_span_err(
span,
&format!(
"cannot specialize on trait `{}`",
tcx.def_path_str(pred.def_id()),
),
)
.emit()
}
}
_ => tcx
.sess
.struct_span_err(span, &format!("cannot specialize on `{:?}`", predicate))
.emit(),
}
}
fn trait_predicate_kind<'tcx>(
tcx: TyCtxt<'tcx>,
predicate: &ty::Predicate<'tcx>,
) -> Option<TraitSpecializationKind> {
match predicate {
ty::Predicate::Trait(pred, hir::Constness::NotConst) => {
Some(tcx.trait_def(pred.def_id()).specialization_kind)
}
ty::Predicate::Trait(_, hir::Constness::Const)
| ty::Predicate::RegionOutlives(_)
| ty::Predicate::TypeOutlives(_)
| ty::Predicate::Projection(_)
| ty::Predicate::WellFormed(_)
| ty::Predicate::Subtype(_)
| ty::Predicate::ObjectSafe(_)
| ty::Predicate::ClosureKind(..)
| ty::Predicate::ConstEvaluatable(..) => None,
}
}

View File

@ -64,6 +64,7 @@ This API is completely unstable and subject to change.
#![feature(nll)]
#![feature(try_blocks)]
#![feature(never_type)]
#![feature(slice_partition_dedup)]
#![recursion_limit = "256"]
#[macro_use]

View File

@ -0,0 +1,32 @@
// Test that associated types in trait objects are not considered to be
// constrained.
#![feature(min_specialization)]
trait Specializable {
fn f();
}
trait B<T> {
type Y;
}
trait C {
type Y;
}
impl<A: ?Sized> Specializable for A {
default fn f() {}
}
impl<'a, T> Specializable for dyn B<T, Y = T> + 'a {
//~^ ERROR specializing impl repeats parameter `T`
fn f() {}
}
impl<'a, T> Specializable for dyn C<Y = (T, T)> + 'a {
//~^ ERROR specializing impl repeats parameter `T`
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,20 @@
error: specializing impl repeats parameter `T`
--> $DIR/dyn-trait-assoc-types.rs:22:1
|
LL | / impl<'a, T> Specializable for dyn B<T, Y = T> + 'a {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: specializing impl repeats parameter `T`
--> $DIR/dyn-trait-assoc-types.rs:27:1
|
LL | / impl<'a, T> Specializable for dyn C<Y = (T, T)> + 'a {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: aborting due to 2 previous errors

View File

@ -0,0 +1,24 @@
// Test that projection bounds can't be specialized on.
#![feature(min_specialization)]
trait X {
fn f();
}
trait Id {
type This;
}
impl<T> Id for T {
type This = T;
}
impl<T: Id> X for T {
default fn f() {}
}
impl<I, V: Id<This = (I,)>> X for V {
//~^ ERROR cannot specialize on
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,11 @@
error: cannot specialize on `Binder(ProjectionPredicate(ProjectionTy { substs: [V], item_def_id: DefId(0:6 ~ repeated_projection_type[317d]::Id[0]::This[0]) }, (I,)))`
--> $DIR/repeated_projection_type.rs:19:1
|
LL | / impl<I, V: Id<This = (I,)>> X for V {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: aborting due to previous error

View File

@ -0,0 +1,19 @@
// Test that directly specializing on repeated lifetime parameters is not
// allowed.
#![feature(min_specialization)]
trait X {
fn f();
}
impl<T> X for T {
default fn f() {}
}
impl<'a> X for (&'a u8, &'a u8) {
//~^ ERROR specializing impl repeats parameter `'a`
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,11 @@
error: specializing impl repeats parameter `'a`
--> $DIR/repeating_lifetimes.rs:14:1
|
LL | / impl<'a> X for (&'a u8, &'a u8) {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: aborting due to previous error

View File

@ -0,0 +1,17 @@
// Test that specializing on two type parameters being equal is not allowed.
#![feature(min_specialization)]
trait X {
fn f();
}
impl<T> X for T {
default fn f() {}
}
impl<T> X for (T, T) {
//~^ ERROR specializing impl repeats parameter `T`
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,11 @@
error: specializing impl repeats parameter `T`
--> $DIR/repeating_param.rs:12:1
|
LL | / impl<T> X for (T, T) {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: aborting due to previous error

View File

@ -0,0 +1,20 @@
// Check that we can specialize on a concrete iterator type. This requires us
// to consider which parameters in the parent impl are constrained.
// check-pass
#![feature(min_specialization)]
trait SpecFromIter<T> {
fn f(&self);
}
impl<'a, T: 'a, I: Iterator<Item = &'a T>> SpecFromIter<T> for I {
default fn f(&self) {}
}
impl<'a, T> SpecFromIter<T> for std::slice::Iter<'a, T> {
fn f(&self) {}
}
fn main() {}

View File

@ -0,0 +1,19 @@
// Check that lifetime parameters are allowed in specializing impls.
// check-pass
#![feature(min_specialization)]
trait MySpecTrait {
fn f();
}
impl<T> MySpecTrait for T {
default fn f() {}
}
impl<'a, T: ?Sized> MySpecTrait for &'a T {
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,17 @@
// Test that `rustc_unsafe_specialization_marker` is only allowed on marker traits.
#![feature(rustc_attrs)]
#[rustc_unsafe_specialization_marker]
trait SpecMarker {
fn f();
//~^ ERROR marker traits
}
#[rustc_unsafe_specialization_marker]
trait SpecMarker2 {
type X;
//~^ ERROR marker traits
}
fn main() {}

View File

@ -0,0 +1,15 @@
error[E0714]: marker traits cannot have associated items
--> $DIR/specialization_marker.rs:7:5
|
LL | fn f();
| ^^^^^^^
error[E0714]: marker traits cannot have associated items
--> $DIR/specialization_marker.rs:13:5
|
LL | type X;
| ^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0714`.

View File

@ -0,0 +1,26 @@
// Test that `rustc_specialization_trait` requires always applicable impls.
#![feature(min_specialization)]
#![feature(rustc_attrs)]
#[rustc_specialization_trait]
trait SpecMarker {
fn f();
}
impl SpecMarker for &'static u8 {
//~^ ERROR cannot specialize
fn f() {}
}
impl<T> SpecMarker for (T, T) {
//~^ ERROR specializing impl
fn f() {}
}
impl<T: Clone> SpecMarker for [T] {
//~^ ERROR cannot specialize
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,29 @@
error: cannot specialize on `'static` lifetime
--> $DIR/specialization_trait.rs:11:1
|
LL | / impl SpecMarker for &'static u8 {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: specializing impl repeats parameter `T`
--> $DIR/specialization_trait.rs:16:1
|
LL | / impl<T> SpecMarker for (T, T) {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: cannot specialize on trait `std::clone::Clone`
--> $DIR/specialization_trait.rs:21:1
|
LL | / impl<T: Clone> SpecMarker for [T] {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: aborting due to 3 previous errors

View File

@ -0,0 +1,24 @@
// Test that specializing on a `rustc_unsafe_specialization_marker` trait is
// allowed.
// check-pass
#![feature(min_specialization)]
#![feature(rustc_attrs)]
#[rustc_unsafe_specialization_marker]
trait SpecMarker {}
trait X {
fn f();
}
impl<T> X for T {
default fn f() {}
}
impl<T: SpecMarker> X for T {
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,27 @@
// Test that specializing on a `rustc_specialization_trait` trait is allowed.
// check-pass
#![feature(min_specialization)]
#![feature(rustc_attrs)]
#[rustc_specialization_trait]
trait SpecTrait {
fn g(&self);
}
trait X {
fn f(&self);
}
impl<T> X for T {
default fn f(&self) {}
}
impl<T: SpecTrait> X for T {
fn f(&self) {
self.g();
}
}
fn main() {}

View File

@ -0,0 +1,18 @@
// Test that directly specializing on `'static` is not allowed.
#![feature(min_specialization)]
trait X {
fn f();
}
impl<T> X for &'_ T {
default fn f() {}
}
impl X for &'static u8 {
//~^ ERROR cannot specialize on `'static` lifetime
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,11 @@
error: cannot specialize on `'static` lifetime
--> $DIR/specialize_on_static.rs:13:1
|
LL | / impl X for &'static u8 {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: aborting due to previous error

View File

@ -0,0 +1,20 @@
// Test that specializing on a trait is not allowed in general.
#![feature(min_specialization)]
trait SpecMarker {}
trait X {
fn f();
}
impl<T> X for T {
default fn f() {}
}
impl<T: SpecMarker> X for T {
//~^ ERROR cannot specialize on trait `SpecMarker`
fn f() {}
}
fn main() {}

View File

@ -0,0 +1,11 @@
error: cannot specialize on trait `SpecMarker`
--> $DIR/specialize_on_trait.rs:15:1
|
LL | / impl<T: SpecMarker> X for T {
LL | |
LL | | fn f() {}
LL | | }
| |_^
error: aborting due to previous error