Skip to content

Commit 0e20155

Browse files
more nits
1 parent ca49a37 commit 0e20155

File tree

5 files changed

+65
-49
lines changed

5 files changed

+65
-49
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+12-29
Original file line numberDiff line numberDiff line change
@@ -4433,42 +4433,25 @@ declare_lint! {
44334433
/// ```rust,compile_fail
44344434
/// #![deny(coinductive_overlap_in_coherence)]
44354435
///
4436-
/// use std::borrow::Borrow;
4437-
/// use std::cmp::Ordering;
4438-
/// use std::marker::PhantomData;
4436+
/// trait CyclicTrait {}
4437+
/// impl<T: CyclicTrait> CyclicTrait for T {}
44394438
///
4440-
/// #[derive(PartialEq, Default)]
4441-
/// pub(crate) struct Interval<T>(T);
4442-
///
4443-
/// impl<T, Q> PartialEq<Q> for Interval<T>
4444-
/// where
4445-
/// Q: PartialOrd,
4446-
/// {
4447-
/// fn eq(&self, other: &Q) -> bool {
4448-
/// todo!()
4449-
/// }
4450-
/// }
4451-
///
4452-
/// impl<T, Q> PartialOrd<Q> for Interval<T>
4453-
/// where
4454-
/// Q: PartialOrd,
4455-
/// {
4456-
/// fn partial_cmp(&self, other: &Q) -> Option<Ordering> {
4457-
/// todo!()
4458-
/// }
4459-
/// }
4439+
/// trait Trait {}
4440+
/// impl<T: CyclicTrait> Trait for T {}
4441+
/// // conflicting impl with the above
4442+
/// impl Trait for u8 {}
44604443
/// ```
44614444
///
44624445
/// {{produces}}
44634446
///
44644447
/// ### Explanation
44654448
///
4466-
/// The manual impl of `PartialEq` impl overlaps with the `derive`, since
4467-
/// if we replace `Q = Interval<T>`, then the second impl leads to a cycle:
4468-
/// `PartialOrd for Interval<T> where Interval<T>: PartialOrd`. This cycle
4469-
/// currently causes the compiler to consider `Interval<T>: PartialOrd` to not
4470-
/// hold, causing the two implementations to be disjoint. This will change in
4471-
/// a future release.
4449+
/// We have two choices for impl which satisfy `u8: Trait`: the blanket impl
4450+
/// for generic `T`, and the direct impl for `u8`. These two impls nominally
4451+
/// overlap, since we can infer `T = u8` in the former impl, but since the where
4452+
/// clause `u8: CyclicTrait` would end up resulting in a cycle (since it depends
4453+
/// on itself), the blanket impl is not considered to hold for `u8`. This will
4454+
/// change in a future release.
44724455
pub COINDUCTIVE_OVERLAP_IN_COHERENCE,
44734456
Warn,
44744457
"impls that are not considered to overlap may be considered to \

compiler/rustc_trait_selection/src/traits/coherence.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,29 @@ fn overlap<'tcx>(
231231
COINDUCTIVE_OVERLAP_IN_COHERENCE,
232232
infcx.tcx.local_def_id_to_hir_id(first_local_impl),
233233
infcx.tcx.def_span(first_local_impl),
234-
"impls that are not considered to overlap may be considered to \
235-
overlap in the future",
234+
format!(
235+
"implementations {} will conflict in the future",
236+
match impl1_header.trait_ref {
237+
Some(trait_ref) => {
238+
let trait_ref = infcx.resolve_vars_if_possible(trait_ref);
239+
format!(
240+
"of `{}` for `{}`",
241+
trait_ref.print_only_trait_path(),
242+
trait_ref.self_ty()
243+
)
244+
}
245+
None => format!(
246+
"for `{}`",
247+
infcx.resolve_vars_if_possible(impl1_header.self_ty)
248+
),
249+
},
250+
),
236251
|lint| {
237-
lint.span_label(
252+
lint.note(
253+
"impls that are not considered to overlap may be considered to \
254+
overlap in the future",
255+
)
256+
.span_label(
238257
infcx.tcx.def_span(impl1_header.impl_def_id),
239258
"the first impl is here",
240259
)
@@ -245,8 +264,12 @@ fn overlap<'tcx>(
245264
if !failing_obligation.cause.span.is_dummy() {
246265
lint.span_label(
247266
failing_obligation.cause.span,
248-
"this where-clause causes a cycle, but it may be treated \
249-
as possibly holding in the future, causing the impls to overlap",
267+
format!(
268+
"`{}` may be considered to hold in future releases, \
269+
causing the impls to overlap",
270+
infcx
271+
.resolve_vars_if_possible(failing_obligation.predicate)
272+
),
250273
);
251274
}
252275
lint

compiler/rustc_trait_selection/src/traits/select/mod.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,25 @@ enum BuiltinImplConditions<'tcx> {
202202

203203
#[derive(Copy, Clone)]
204204
pub enum TreatInductiveCycleAs {
205+
/// This is the previous behavior, where `Recur` represents an inductive
206+
/// cycle that is known not to hold. This is not forwards-compatible with
207+
/// coinduction, and will be deprecated. This is the default behavior
208+
/// of the old trait solver due to back-compat reasons.
205209
Recur,
210+
/// This is the behavior of the new trait solver, where inductive cycles
211+
/// are treated as ambiguous and possibly holding.
206212
Ambig,
207213
}
208214

215+
impl From<TreatInductiveCycleAs> for EvaluationResult {
216+
fn from(treat: TreatInductiveCycleAs) -> EvaluationResult {
217+
match treat {
218+
TreatInductiveCycleAs::Ambig => EvaluatedToUnknown,
219+
TreatInductiveCycleAs::Recur => EvaluatedToRecur,
220+
}
221+
}
222+
}
223+
209224
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
210225
pub fn new(infcx: &'cx InferCtxt<'tcx>) -> SelectionContext<'cx, 'tcx> {
211226
SelectionContext {
@@ -223,6 +238,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
223238
treat_inductive_cycle: TreatInductiveCycleAs,
224239
f: impl FnOnce(&mut Self) -> T,
225240
) -> T {
241+
// Should be executed in a context where caching is disabled,
242+
// otherwise the cache is poisoned with the temporary result.
243+
assert!(self.is_intercrate());
226244
let treat_inductive_cycle =
227245
std::mem::replace(&mut self.treat_inductive_cycle, treat_inductive_cycle);
228246
let value = f(self);
@@ -741,10 +759,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
741759
stack.update_reached_depth(stack_arg.1);
742760
return Ok(EvaluatedToOk);
743761
} else {
744-
match self.treat_inductive_cycle {
745-
TreatInductiveCycleAs::Ambig => return Ok(EvaluatedToUnknown),
746-
TreatInductiveCycleAs::Recur => return Ok(EvaluatedToRecur),
747-
}
762+
return Ok(self.treat_inductive_cycle.into());
748763
}
749764
}
750765
return Ok(EvaluatedToOk);
@@ -862,10 +877,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
862877
}
863878
}
864879
ProjectAndUnifyResult::FailedNormalization => Ok(EvaluatedToAmbig),
865-
ProjectAndUnifyResult::Recursive => match self.treat_inductive_cycle {
866-
TreatInductiveCycleAs::Ambig => return Ok(EvaluatedToUnknown),
867-
TreatInductiveCycleAs::Recur => return Ok(EvaluatedToRecur),
868-
},
880+
ProjectAndUnifyResult::Recursive => Ok(self.treat_inductive_cycle.into()),
869881
ProjectAndUnifyResult::MismatchedProjectionTypes(_) => Ok(EvaluatedToErr),
870882
}
871883
}
@@ -1179,10 +1191,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11791191
Some(EvaluatedToOk)
11801192
} else {
11811193
debug!("evaluate_stack --> recursive, inductive");
1182-
match self.treat_inductive_cycle {
1183-
TreatInductiveCycleAs::Ambig => Some(EvaluatedToUnknown),
1184-
TreatInductiveCycleAs::Recur => Some(EvaluatedToRecur),
1185-
}
1194+
Some(self.treat_inductive_cycle.into())
11861195
}
11871196
} else {
11881197
None

tests/ui/coherence/warn-when-cycle-is-error-in-coherence.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) struct Interval<T>(PhantomData<T>);
1111
// `Interval<?1>: PartialOrd<Interval<?1>>` candidate which results
1212
// in a - currently inductive - cycle.
1313
impl<T, Q> PartialEq<Q> for Interval<T>
14-
//~^ ERROR impls that are not considered to overlap may be considered to overlap in the future
14+
//~^ ERROR implementations of `PartialEq<Interval<_>>` for `Interval<_>` will conflict in the future
1515
//~| WARN this was previously accepted by the compiler but is being phased out
1616
where
1717
T: Borrow<Q>,

tests/ui/coherence/warn-when-cycle-is-error-in-coherence.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: impls that are not considered to overlap may be considered to overlap in the future
1+
error: implementations of `PartialEq<Interval<_>>` for `Interval<_>` will conflict in the future
22
--> $DIR/warn-when-cycle-is-error-in-coherence.rs:13:1
33
|
44
LL | #[derive(PartialEq, Default)]
@@ -8,10 +8,11 @@ LL | impl<T, Q> PartialEq<Q> for Interval<T>
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the first impl is here
99
...
1010
LL | Q: ?Sized + PartialOrd,
11-
| ---------- this where-clause causes a cycle, but it may be treated as possibly holding in the future, causing the impls to overlap
11+
| ---------- `Interval<_>: PartialOrd` may be considered to hold in future releases, causing the impls to overlap
1212
|
1313
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1414
= note: for more information, see issue #114040 <https://github.com/rust-lang/rust/issues/114040>
15+
= note: impls that are not considered to overlap may be considered to overlap in the future
1516
note: the lint level is defined here
1617
--> $DIR/warn-when-cycle-is-error-in-coherence.rs:1:9
1718
|

0 commit comments

Comments
 (0)