-
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 #114023 - compiler-errors:coinductive-cycle-lint, r=lcnr
Warn on inductive cycle in coherence leading to impls being considered not overlapping This PR implements a `coinductive_overlap_in_coherence` lint (#114040), which warns users against cases where two impls are considered **not** to overlap during coherence due to an inductive cycle disproving one of the predicates after unifying the two impls. Cases where this lint fires will become an overlap error if we ever move to coinduction, so I'd like to make this a warning to avoid having more crates take advantage of this behavior in the mean time. Also, since the new trait solver treats inductive cycles as ambiguity, not an error, this is a blocker for landing the new trait solver in coherence.
- Loading branch information
Showing
10 changed files
with
252 additions
and
54 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
35 changes: 35 additions & 0 deletions
35
tests/ui/coherence/warn-when-cycle-is-error-in-coherence.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,35 @@ | ||
#![deny(coinductive_overlap_in_coherence)] | ||
|
||
use std::borrow::Borrow; | ||
use std::cmp::Ordering; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(PartialEq, Default)] | ||
pub(crate) struct Interval<T>(PhantomData<T>); | ||
|
||
// This impl overlaps with the `derive` unless we reject the nested | ||
// `Interval<?1>: PartialOrd<Interval<?1>>` candidate which results | ||
// in a - currently inductive - cycle. | ||
impl<T, Q> PartialEq<Q> for Interval<T> | ||
//~^ ERROR implementations of `PartialEq<Interval<_>>` for `Interval<_>` will conflict in the future | ||
//~| WARN this was previously accepted by the compiler but is being phased out | ||
where | ||
T: Borrow<Q>, | ||
Q: ?Sized + PartialOrd, | ||
{ | ||
fn eq(&self, _: &Q) -> bool { | ||
true | ||
} | ||
} | ||
|
||
impl<T, Q> PartialOrd<Q> for Interval<T> | ||
where | ||
T: Borrow<Q>, | ||
Q: ?Sized + PartialOrd, | ||
{ | ||
fn partial_cmp(&self, _: &Q) -> Option<Ordering> { | ||
None | ||
} | ||
} | ||
|
||
fn main() {} |
23 changes: 23 additions & 0 deletions
23
tests/ui/coherence/warn-when-cycle-is-error-in-coherence.stderr
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,23 @@ | ||
error: implementations of `PartialEq<Interval<_>>` for `Interval<_>` will conflict in the future | ||
--> $DIR/warn-when-cycle-is-error-in-coherence.rs:13:1 | ||
| | ||
LL | #[derive(PartialEq, Default)] | ||
| --------- the second impl is here | ||
... | ||
LL | impl<T, Q> PartialEq<Q> for Interval<T> | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the first impl is here | ||
... | ||
LL | Q: ?Sized + PartialOrd, | ||
| ---------- `Interval<_>: PartialOrd` may be considered to hold in future releases, causing the impls to overlap | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #114040 <https://github.com/rust-lang/rust/issues/114040> | ||
= note: impls that are not considered to overlap may be considered to overlap in the future | ||
note: the lint level is defined here | ||
--> $DIR/warn-when-cycle-is-error-in-coherence.rs:1:9 | ||
| | ||
LL | #![deny(coinductive_overlap_in_coherence)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
Oops, something went wrong.