Skip to content

Commit

Permalink
Properly check for lifetime equality instead of relying on identity
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Oct 20, 2023
1 parent 9b1fcee commit 29b10dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
23 changes: 12 additions & 11 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_infer::infer::TyCtxtInferExt as _;
use rustc_infer::traits::{Obligation, ObligationCause};
use rustc_middle::traits::DefiningAnchor;
use rustc_middle::ty::visit::TypeVisitableExt;
use rustc_middle::ty::RegionVid;
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
use rustc_middle::ty::{GenericArgKind, GenericArgs};
use rustc_span::Span;
Expand All @@ -26,13 +27,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
.find_map(|lb| self.eval_equal(vid, lb).then_some(self.definitions[lb].external_name?))
}

fn gen_arg_to_name(&self, arg: ty::GenericArg<'tcx>) -> Option<ty::Region<'tcx>> {
fn generic_arg_to_region(&self, arg: ty::GenericArg<'tcx>) -> Option<RegionVid> {
let region = arg.as_region()?;

if let ty::RePlaceholder(..) = region.kind() {
return None;
None
} else {
Some(self.to_region_vid(region))
}
self.universal_name(self.to_region_vid(region))
}

/// Check that all opaque types have the same region parameters if they have the same
Expand All @@ -54,19 +56,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
}
trace!(?a, ?b);
for (a, b) in a.args.iter().zip(b.args) {
if a.as_region().is_none() {
break;
}
let a = self.gen_arg_to_name(a).unwrap();
let b = self.gen_arg_to_name(b).unwrap();
trace!(?a, ?b);
if a == b {
let Some(r1) = self.generic_arg_to_region(a) else {
continue;
};
let r2 = self.generic_arg_to_region(b).unwrap();
if self.eval_equal(r1, r2) {
continue;
}

infcx.tcx.sess.emit_err(LifetimeMismatchOpaqueParam {
arg: a.into(),
prev: b.into(),
arg: self.universal_name(r1).unwrap().into(),
prev: self.universal_name(r2).unwrap().into(),
span: a_ty.span,
prev_span: b_ty.span,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ fn f<A: ToString + Clone, B: ToString + Clone>(a: A, b: B) -> (X<A, B>, X<A, B>)
(a.clone(), a)
}

type Tait<'x> = impl Sized;
fn define<'a: 'b, 'b: 'a>(x: &'a u8, y: &'b u8) -> (Tait<'a>, Tait<'b>) {
((), ())
}

fn main() {
println!("{}", <X<_, _> as ToString>::to_string(&f(42_i32, String::new()).1));
}

0 comments on commit 29b10dc

Please sign in to comment.