mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 07:14:28 +00:00
Opaques do not constrain generic params
This commit is contained in:
parent
f1b854818d
commit
d0c826cfc2
@ -59,7 +59,7 @@ struct ParameterCollector {
|
||||
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
|
||||
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
||||
match *t.kind() {
|
||||
ty::Alias(ty::Projection | ty::Inherent, ..) if !self.include_nonconstraining => {
|
||||
ty::Alias(..) if !self.include_nonconstraining => {
|
||||
// projections are not injective
|
||||
return ControlFlow::Continue(());
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ fn use_alias<T>(val: T) -> AliasOfForeignType<T> {
|
||||
}
|
||||
|
||||
impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
|
||||
//~^ ERROR only traits defined in the current crate can be implemented for arbitrary types
|
||||
//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates
|
||||
|
||||
fn main() {}
|
||||
|
@ -1,14 +1,9 @@
|
||||
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
|
||||
--> $DIR/coherence.rs:14:1
|
||||
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
|
||||
--> $DIR/coherence.rs:14:6
|
||||
|
|
||||
LL | impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------
|
||||
| | |
|
||||
| | `AliasOfForeignType<T>` is not defined in the current crate
|
||||
| impl doesn't use only types from inside the current crate
|
||||
|
|
||||
= note: define and implement a trait or new type instead
|
||||
| ^ unconstrained type parameter
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0117`.
|
||||
For more information about this error, try `rustc --explain E0207`.
|
||||
|
@ -1,7 +1,6 @@
|
||||
// check-pass
|
||||
|
||||
// FIXME(type_alias_impl_trait): What does this test? This needs a comment
|
||||
// explaining what we're worried about here.
|
||||
|
||||
#![feature(type_alias_impl_trait)]
|
||||
trait Trait {}
|
||||
type Opaque<T> = impl Sized;
|
||||
@ -11,5 +10,6 @@ fn foo<T>() -> Opaque<T> {
|
||||
|
||||
impl<T, V> Trait for (T, V, V, u32) {}
|
||||
impl<U, V> Trait for (Opaque<U>, V, i32, V) {}
|
||||
//~^ ERROR the type parameter `U` is not constrained by the impl trait, self type, or predicates
|
||||
|
||||
fn main() {}
|
||||
|
@ -0,0 +1,9 @@
|
||||
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
|
||||
--> $DIR/coherence_generalization.rs:12:6
|
||||
|
|
||||
LL | impl<U, V> Trait for (Opaque<U>, V, i32, V) {}
|
||||
| ^ unconstrained type parameter
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0207`.
|
25
tests/ui/type-alias-impl-trait/unconstrained-impl-param.rs
Normal file
25
tests/ui/type-alias-impl-trait/unconstrained-impl-param.rs
Normal file
@ -0,0 +1,25 @@
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
type Opaque<'a> = impl Sized + 'static;
|
||||
fn define<'a>() -> Opaque<'a> {}
|
||||
|
||||
trait Trait {
|
||||
type Assoc: Display;
|
||||
}
|
||||
impl<'a> Trait for Opaque<'a> {
|
||||
//~^ ERROR the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
|
||||
type Assoc = &'a str;
|
||||
}
|
||||
|
||||
// ======= Exploit =======
|
||||
|
||||
fn extend<T: Trait + 'static>(s: T::Assoc) -> Box<dyn Display> {
|
||||
Box::new(s)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let val = extend::<Opaque<'_>>(&String::from("blah blah blah"));
|
||||
println!("{}", val);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
|
||||
--> $DIR/unconstrained-impl-param.rs:11:6
|
||||
|
|
||||
LL | impl<'a> Trait for Opaque<'a> {
|
||||
| ^^ unconstrained lifetime parameter
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0207`.
|
Loading…
Reference in New Issue
Block a user