-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #24056 - nikomatsakis:issue-23853-crates-io, r=aturon
If we find a blanket impl for `Trait` but we're matching on an object `Trait`, prefer the object (I think we could perhaps go either way, but this seems safer). Also give a nice error for attempts to manually `impl Trait for Trait`, since they will be ineffectual. This fixes the problems around ambiguity ICEs relating to `Any` and `MarkerTrait` that were cropping up all over the place. There may still be similar ICEs reported in #21756 that this PR does not address. Fixes #24015. Fixes #24051. Fixes #24037. Fixes #23853. Fixes #21942. cc #21756. cc @alexcrichton (this fixes crates.io) r? @aturon
- Loading branch information
Showing
5 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Test that we give suitable error messages when the user attempts to | ||
// impl a trait `Trait` for its own object type. | ||
|
||
trait Foo { fn dummy(&self) { } } | ||
trait Bar: Foo { } | ||
trait Baz: Bar { } | ||
|
||
// Subtraits of Baz are not legal: | ||
impl Foo for Baz { } //~ ERROR E0371 | ||
impl Bar for Baz { } //~ ERROR E0371 | ||
impl Baz for Baz { } //~ ERROR E0371 | ||
|
||
// But other random traits are: | ||
trait Other { } | ||
impl Other for Baz { } // OK, Bar not a subtrait of Baz | ||
|
||
// If the trait is not object-safe, we give a more tailored message | ||
// because we're such schnuckels: | ||
trait NotObjectSafe { fn eq(&self, other: Self); } | ||
impl NotObjectSafe for NotObjectSafe { } //~ ERROR E0372 | ||
|
||
fn main() { } |
27 changes: 27 additions & 0 deletions
27
src/test/run-pass/traits-impl-object-overlap-issue-23853.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Test that we are able to compile the case where both a blanket impl | ||
// and the object type itself supply the required trait obligation. | ||
// In this case, the blanket impl for `Foo` applies to any type, | ||
// including `Bar`, but the object type `Bar` also implicitly supplies | ||
// this context. | ||
|
||
trait Foo { fn dummy(&self) { } } | ||
|
||
trait Bar: Foo { } | ||
|
||
impl<T:?Sized> Foo for T { } | ||
|
||
fn want_foo<B:?Sized+Foo>() { } | ||
|
||
fn main() { | ||
want_foo::<Bar>(); | ||
} |