Skip to content

prevent specialized negative impls #74648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 83 additions & 16 deletions src/librustc_typeck/check/dropck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,24 @@ use rustc_trait_selection::traits::{ObligationCause, TraitEngine, TraitEngineExt
pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), ErrorReported> {
check_restricted_impl(tcx, drop_impl_did)
}

pub fn check_restricted_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), ErrorReported> {
let dtor_self_type = tcx.type_of(drop_impl_did);
let dtor_predicates = tcx.predicates_of(drop_impl_did);
match dtor_self_type.kind {

let check_empty_where_bounds = |s| {
if dtor_predicates.predicates.is_empty() {
Ok(())
} else {
tcx.sess.span_err(
tcx.def_span(drop_impl_did),
&format!("negative impls on {} must not contain where bounds", s),
);
Err(ErrorReported)
}
};

let kind_str = match dtor_self_type.kind {
ty::Adt(adt_def, self_to_impl_substs) => {
ensure_drop_params_and_item_params_correspond(
tcx,
Expand All @@ -46,29 +60,82 @@ pub fn check_restricted_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<()
adt_def.did,
)?;

ensure_drop_predicates_are_implied_by_item_defn(
return ensure_drop_predicates_are_implied_by_item_defn(
tcx,
drop_impl_did.expect_local(),
dtor_predicates,
adt_def.did,
self_to_impl_substs,
)
);
}
_ => {
_ if tcx.lang_items().drop_trait()
== Some(tcx.impl_trait_ref(drop_impl_did).unwrap().def_id) =>
{
// Destructors only work on nominal types. This was
// already checked by coherence, for auto impls we
// simply check that there are no bounds here.
if dtor_predicates.predicates.is_empty() {
Ok(())
} else {
tcx.sess.span_err(
tcx.def_span(drop_impl_did),
"auto traits must not contain where bounds",
);
Err(ErrorReported)
}
// already checked by coherence, but compilation may
// not have been terminated.
tcx.sess.delay_span_bug(
tcx.def_span(drop_impl_did),
&format!("should have been rejected by coherence check: {}", dtor_self_type),
);
return Err(ErrorReported);
}
}
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => {
return check_empty_where_bounds("primitive types");
}
ty::Foreign(_) => {
return check_empty_where_bounds("foreign types");
}
ty::Array(..) => {
// FIXME: Support negative impls for `[T; N]` where `T: Sized` is the only predicate.
"arrays"
}
ty::Slice(..) => {
// FIXME: Support negative impls for `[T]` where `T: Sized` is the only predicate.
"slices"
}
ty::RawPtr(..) => {
return check_empty_where_bounds("raw pointers");
}
ty::Ref(..) => {
return check_empty_where_bounds("references");
}
ty::FnPtr(..) => {
// In case we ever get variadic functions allowing this would mean that we have
// specialized negative impls on stable.
"function pointers"
}
ty::Dynamic(..) => {
return check_empty_where_bounds("trait objects");
}
ty::Never => {
return check_empty_where_bounds("`!`");
}
ty::Tuple(..) => {
// In case we ever get variadic tuples allowing this would mean that we have
// specialized negative impls on stable.
"tuples"
}
ty::Projection(..) => "projections",
ty::Opaque(..) => "opaque types",
ty::Param(..) => {
return check_empty_where_bounds("type parameters");
}
ty::Error(..) => return Ok(()),
ty::FnDef(..)
| ty::Closure(..)
| ty::Generator(..)
| ty::GeneratorWitness(..)
| ty::Bound(..)
| ty::Placeholder(..)
| ty::Infer(..) => bug!("unexpected impl self ty: {:?}", dtor_self_type),
};

tcx.sess.span_err(
tcx.def_span(drop_impl_did),
&format!("negative impls are not allowed for {}", kind_str),
);
Err(ErrorReported)
}

fn ensure_drop_params_and_item_params_correspond<'tcx>(
Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/issues/issue-29516.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

auto trait NotSame {}

impl<A> !NotSame for (A, A) {} //~ ERROR auto traits must not contain where bounds
// FIXME: Consider allowing (A, B) with `A: Sized`.
impl<A> !NotSame for (A, A) {} //~ ERROR negative impls are not allowed for tuples

trait OneOfEach {}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-29516.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: auto traits must not contain where bounds
error: negative impls are not allowed for tuples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we disallow negative impls for all tuples? (I think that'd be ok, actually, though I think we could also permit them for tuples if they are only restricted based on arity, but I don't see the point.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think allowing them for tuples will prevent us from ever adding variadic tuples, so it should be easier to just disallow them for now

--> $DIR/issue-29516.rs:6:1
|
LL | impl<A> !NotSame for (A, A) {}
Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/issues/issue-41974.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ struct Flags;
trait A {}

impl<T> Drop for T where T: A {
//~^ ERROR auto traits must not contain where bounds
//~| ERROR E0119
//~^ ERROR E0119
//~| ERROR E0120
//~| ERROR E0210
fn drop(&mut self) {}
Expand Down
14 changes: 1 addition & 13 deletions src/test/ui/issues/issue-41974.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ error[E0120]: the `Drop` trait may only be implemented for structs, enums, and u
LL | impl<T> Drop for T where T: A {
| ^ must be a struct, enum, or union

error: auto traits must not contain where bounds
--> $DIR/issue-41974.rs:6:1
|
LL | / impl<T> Drop for T where T: A {
LL | |
LL | |
LL | |
LL | |
LL | | fn drop(&mut self) {}
LL | | }
| |_^

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
--> $DIR/issue-41974.rs:6:6
|
Expand All @@ -36,7 +24,7 @@ LL | impl<T> Drop for T where T: A {
= note: implementing a foreign trait is only possible if at least one of the types for which is it implemented is local
= note: only traits defined in the current crate can be implemented for a type parameter

error: aborting due to 4 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0119, E0120, E0210.
For more information about an error, try `rustc --explain E0119`.
3 changes: 2 additions & 1 deletion src/test/ui/traits/negative-impls/negative-default-impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ trait MyTrait {
type Foo;
}

default impl !MyTrait for u32 {} //~ ERROR auto traits must not contain where bounds
default impl !MyTrait for u32 {}
//~^ ERROR negative impls on primitive types must not contain where bounds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the "where bound" here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like it's more about the default keyword, perhaps?


fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | #![feature(specialization)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information

error: auto traits must not contain where bounds
error: negative impls on primitive types must not contain where bounds
--> $DIR/negative-default-impls.rs:9:1
|
LL | default impl !MyTrait for u32 {}
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/traits/negative-impls/negative-impl-sized.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![feature(negative_impls)]

// Test a negative impl for a trait requires `T: ?Sized`.
// Test that negative impls for a trait requires `T: ?Sized`.

trait MyTrait {}

impl<T> !MyTrait for T {} //~ ERROR auto traits must not contain where bounds
impl<T> !MyTrait for T {}
//~^ ERROR negative impls on type parameters must not contain where bounds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any way to highlight the T: Sized bound? e.g., for drop we print something like

error[E0367]: Drop impl requires T: std::marker::Sized but the struct it is implemented for does not


fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: auto traits must not contain where bounds
error: negative impls on type parameters must not contain where bounds
--> $DIR/negative-impl-sized.rs:7:1
|
LL | impl<T> !MyTrait for T {}
Expand Down