Skip to content

Commit b9da4e3

Browse files
authored
Merge pull request #1601 from Nadrieril/more-tweaks
More mixed fixes in lifetime handling
2 parents b7182db + 52e00d7 commit b9da4e3

26 files changed

Lines changed: 415 additions & 159 deletions

‎charon/src/ast/type_level.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,13 @@ impl OutlivesPred {
287287
}
288288
}
289289

290-
fn enter_ty(&mut self, ty: &Ty) {
290+
fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break> {
291291
if matches!(ty.kind(), TyKind::TypeVar(_) | TyKind::TraitType(..)) {
292292
self.enter_region(&Region::Lub(ty.clone()));
293+
// `T::Assoc: 'a` does not imply `T: 'a`.
294+
return ControlFlow::Continue(());
293295
}
296+
self.visit_inner(ty)
294297
}
295298
}
296299

‎charon/src/bin/charon-driver/translate/translate_closures.rs‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -526,35 +526,33 @@ impl<'tcx> ItemTransCtx<'tcx, '_> {
526526
// arg-N, rest...
527527
// We then add N statements of the form `locals[N+3] := move locals[2].N`,
528528
// to destructure the arguments.
529-
let Body::Unstructured(GExprBody {
530-
locals,
531-
body: blocks,
532-
..
533-
}) = &mut body
534-
else {
529+
let Body::Unstructured(inner_body) = &mut body else {
535530
return Ok(body);
536531
};
537532

538-
// The (Arg1, Arg2, ..) type.
539-
let tupled_ty = &signature.inputs[1];
540-
541-
blocks.dyn_visit_mut(|local: &mut LocalId| {
533+
inner_body.dyn_visit_in_body_mut(|local: &mut LocalId| {
542534
if local.index() >= 2 {
543535
*local += 1;
544536
}
545537
});
546538

539+
let GExprBody {
540+
locals,
541+
body: blocks,
542+
..
543+
} = inner_body;
544+
545+
// The (Arg1, Arg2, ..) type.
546+
let tupled_ty = &signature.inputs[1];
547+
547548
// Remember how many arguments there are
548549
let closure_arg_count = locals.arg_count - 1;
549550
let mut old_locals = mem::take(&mut locals.locals).into_iter();
550551
locals.arg_count = 2;
551552
locals.locals.push(old_locals.next().unwrap()); // ret
552553
locals.locals.push(old_locals.next().unwrap()); // state
553554
let tupled_arg = locals.new_var(Some("tupled_args".to_string()), tupled_ty.clone());
554-
locals.locals.extend(old_locals.map(|mut l| {
555-
l.index += 1;
556-
l
557-
}));
555+
locals.locals.extend(old_locals);
558556

559557
let untupled_args = locals
560558
.locals

‎charon/src/bin/charon-driver/translate/user_type_annotations.rs‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ impl UserTypeAnnotations {
5151
return Ok(recovered);
5252
}
5353

54-
// Gather all the projections on user annotations. We always include at least the
55-
// unprojected annotation.
54+
// Gather the projections on user annotations that are actually used. Unused annotation
55+
// types can reference items like intrinsics that have already been lowered away.
5656
struct CollectProjections(
5757
rustc_index::IndexVec<
5858
ty::UserTypeAnnotationIndex,
@@ -63,13 +63,29 @@ impl UserTypeAnnotations {
6363
fn visit_user_type_projection(&mut self, projection: &mir::UserTypeProjection) {
6464
self.0[projection.base].insert(projection.projs.clone());
6565
}
66+
67+
fn visit_const_operand(
68+
&mut self,
69+
operand: &mir::ConstOperand<'tcx>,
70+
location: mir::Location,
71+
) {
72+
if let Some(base) = operand.user_ty {
73+
self.0[base].insert(vec![]);
74+
}
75+
self.super_const_operand(operand, location);
76+
}
77+
78+
fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: mir::Location) {
79+
if let mir::Rvalue::Aggregate(kind, _) = rvalue
80+
&& let mir::AggregateKind::Adt(_, _, _, Some(base), _) = **kind
81+
{
82+
self.0[base].insert(vec![]);
83+
}
84+
self.super_rvalue(rvalue, location);
85+
}
6686
}
67-
let mut projections = CollectProjections(
68-
annotations
69-
.iter()
70-
.map(|_| SeqHashSet::from([vec![]]))
71-
.collect(),
72-
);
87+
let mut projections =
88+
CollectProjections(annotations.iter().map(|_| SeqHashSet::default()).collect());
7389
projections.visit_body(body);
7490
for (base, projections) in projections.0.into_iter_enumerated() {
7591
let annotation = &annotations[base];

‎charon/src/transform/add_missing_info/add_implied_outlives.rs‎

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl OutlivesAtBinder {
3535
#[derive(Visitor)]
3636
struct OutlivesGatherer<'a> {
3737
type_decls: &'a IndexMap<TypeDeclId, TypeDecl>,
38+
trait_decls: &'a IndexMap<TraitDeclId, TraitDecl>,
3839
item_params: &'a mut GenericParams,
3940
binders: BindingStack<OutlivesAtBinder>,
4041
}
@@ -43,9 +44,11 @@ impl<'a> OutlivesGatherer<'a> {
4344
fn new(
4445
item_params: &'a mut GenericParams,
4546
type_decls: &'a IndexMap<TypeDeclId, TypeDecl>,
47+
trait_decls: &'a IndexMap<TraitDeclId, TraitDecl>,
4648
) -> Self {
4749
Self {
4850
type_decls,
51+
trait_decls,
4952
item_params,
5053
binders: BindingStack::new(OutlivesAtBinder::default()),
5154
}
@@ -117,6 +120,16 @@ impl VisitAstMut for OutlivesGatherer<'_> {
117120
}
118121

119122
fn visit_ty(&mut self, ty: &mut Ty) -> ControlFlow<Self::Break> {
123+
if let TyKind::TraitType(proof, type_id, args) = ty.kind()
124+
&& let Some(decl) = self.trait_decls.get(proof.trait_id())
125+
&& let Some(assoc) = decl.types.get(*type_id)
126+
{
127+
// Instantiate the trait parameters under the GAT binder, then the GAT parameters.
128+
let assoc = assoc.clone().substitute_with_tref(proof);
129+
for pred in assoc.params.outlives.substitute_inner_binder(args) {
130+
self.add_outlives(pred);
131+
}
132+
}
120133
match ty.kind() {
121134
TyKind::TypeVar(_) | TyKind::TraitType(..) => {
122135
self.add_outliving_region(&Region::Lub(ty.clone()));
@@ -166,19 +179,24 @@ impl VisitAstMut for OutlivesGatherer<'_> {
166179

167180
struct ClosureOutlivesComputer<'a> {
168181
type_decls: &'a mut IndexMap<TypeDeclId, TypeDecl>,
182+
trait_decls: &'a IndexMap<TraitDeclId, TraitDecl>,
169183
/// Map of types we want to process.
170184
closure_tys: SeqHashMap<TypeDeclId, CycleDetector<()>>,
171185
}
172186

173187
impl<'a> ClosureOutlivesComputer<'a> {
174-
fn new(type_decls: &'a mut IndexMap<TypeDeclId, TypeDecl>) -> Self {
188+
fn new(
189+
type_decls: &'a mut IndexMap<TypeDeclId, TypeDecl>,
190+
trait_decls: &'a IndexMap<TraitDeclId, TraitDecl>,
191+
) -> Self {
175192
let closure_tys = type_decls
176193
.iter()
177194
.filter(|decl| matches!(decl.src, TypeSource::Closure { .. }))
178195
.map(|decl| (decl.def_id, CycleDetector::Unprocessed))
179196
.collect();
180197
Self {
181198
type_decls,
199+
trait_decls,
182200
closure_tys,
183201
}
184202
}
@@ -206,7 +224,7 @@ impl<'a> ClosureOutlivesComputer<'a> {
206224
// Move the kind out so that the crate can inspect the rest of the decls.
207225
let mut kind = mem::replace(&mut self.type_decls[type_id].kind, TypeDeclKind::Opaque);
208226
let mut params = mem::take(&mut self.type_decls[type_id].generics);
209-
let mut visitor = OutlivesGatherer::new(&mut params, self.type_decls);
227+
let mut visitor = OutlivesGatherer::new(&mut params, self.type_decls, self.trait_decls);
210228
visitor.visit(&mut kind);
211229
visitor.finish();
212230
self.type_decls[type_id].generics = params;
@@ -225,41 +243,53 @@ pub struct Transform;
225243
impl TransformPass for Transform {
226244
fn transform_ctx(&self, ctx: &mut TransformCtx) {
227245
let type_decls = &mut ctx.translated.type_decls;
246+
let trait_decls = &mut ctx.translated.trait_decls;
228247

229248
// Rustc gives us explicit outlives for ADTs, but we make fake ADTs for closures, so we
230249
// infer their outlives predicates here. Thankfully they can't be recursive, which makes
231250
// the implementation much easier than having to deal with all ADTs.
232-
ClosureOutlivesComputer::new(type_decls).compute_all();
251+
ClosureOutlivesComputer::new(type_decls, trait_decls).compute_all();
233252

234253
for fun_decl in &mut ctx.translated.fun_decls {
235-
let mut visitor = OutlivesGatherer::new(&mut fun_decl.generics, type_decls);
254+
let mut visitor =
255+
OutlivesGatherer::new(&mut fun_decl.generics, type_decls, trait_decls);
236256
visitor.visit(&mut fun_decl.signature);
237-
if let FunSource::InherentImpl { self_ty } = &mut fun_decl.src {
238-
// The inherent impl header contributes to the implied bounds even if that type is
239-
// not mentioned in the signature.
240-
visitor.visit(self_ty);
257+
// The impl header contributes to the implied bounds even if its types are
258+
// not mentioned in the signature.
259+
match &mut fun_decl.src {
260+
FunSource::InherentImpl { self_ty } => {
261+
visitor.visit(self_ty);
262+
}
263+
FunSource::TraitImpl { trait_ref, .. } => {
264+
visitor.visit(trait_ref);
265+
}
266+
_ => {}
241267
}
242268
visitor.finish();
243269

244270
// Also add implied bounds to the inner binders inside bodies. Implied bounds involving
245271
// only body or signature lifetimes must not be kept; they must be checked by
246272
// borrow-checking.
247-
OutlivesGatherer::new(&mut GenericParams::default(), type_decls)
273+
OutlivesGatherer::new(&mut GenericParams::default(), type_decls, trait_decls)
248274
.visit(&mut fun_decl.body);
249275
}
250276

251277
for timpl in &mut ctx.translated.trait_impls {
252-
let mut visitor = OutlivesGatherer::new(&mut timpl.generics, type_decls);
278+
let mut visitor = OutlivesGatherer::new(&mut timpl.generics, type_decls, trait_decls);
253279
visitor.visit(&mut timpl.impl_trait);
254280
visitor.finish();
255281
}
256282

257-
for trait_decl in &mut ctx.translated.trait_decls {
258-
for method in &mut trait_decl.methods {
259-
let mut visitor = OutlivesGatherer::new(&mut method.params, type_decls);
283+
for trait_id in trait_decls.iter_indices().collect_vec() {
284+
// Keep the associated type declarations available while visiting method signatures.
285+
let mut methods = mem::take(&mut trait_decls[trait_id].methods);
286+
for method in &mut methods {
287+
let mut visitor =
288+
OutlivesGatherer::new(&mut method.params, type_decls, trait_decls);
260289
visitor.visit(&mut method.skip_binder.signature);
261290
visitor.finish();
262291
}
292+
trait_decls[trait_id].methods = methods;
263293
}
264294
}
265295
}

‎charon/tests/ui/arrays.out‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,13 +1377,13 @@ pub fn const_array() -> [u32; 2usize]
13771377
pub fn const_slice()
13781378
{
13791379
let mut _0: (); // return
1380-
let mut _1: &'3 [u32]; // anonymous local
1381-
where _1 :< &'1 [u32];
1382-
let mut _2: &'5 [u32; 2usize]; // anonymous local
1383-
let _3: &'6 [u32; 2usize]; // anonymous local
1384-
let mut _4: &'7 [u32; 2usize]; // anonymous local
1385-
let mut _5: &'8 [u32; 2usize]; // anonymous local
1386-
let mut _6: &'10 [u32; 2usize]; // anonymous local
1380+
let mut _1: &'2 [u32]; // anonymous local
1381+
where _1 :< &'0 [u32];
1382+
let mut _2: &'4 [u32; 2usize]; // anonymous local
1383+
let _3: &'5 [u32; 2usize]; // anonymous local
1384+
let mut _4: &'6 [u32; 2usize]; // anonymous local
1385+
let mut _5: &'7 [u32; 2usize]; // anonymous local
1386+
let mut _6: &'9 [u32; 2usize]; // anonymous local
13871387
let mut _7: [u32; 2usize]; // anonymous local
13881388

13891389
storage_live(_5)

‎charon/tests/ui/borrowck-statements.out‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ where
262262
// Full name: test_crate::destructure
263263
pub fn destructure<'a>(input: &'a Either<(u32, u64)>[{built_in impl Sized for (u32, u64)}]) -> &'a u32
264264
{
265-
let mut _0: &'3 u32; // return
266-
let input: &'5 Either<(u32, u64)>[{built_in impl Sized for (u32, u64)}]; // arg #1
267-
let x: &'6 u32; // local
268-
let mut _3: &'7 Either<(u32, u64)>[{built_in impl Sized for (u32, u64)}]; // anonymous local
269-
where _3 :< &'1 Either<(u32, u64)>[{built_in impl Sized for (u32, u64)}];
265+
let mut _0: &'2 u32; // return
266+
let input: &'4 Either<(u32, u64)>[{built_in impl Sized for (u32, u64)}]; // arg #1
267+
let x: &'5 u32; // local
268+
let mut _3: &'6 Either<(u32, u64)>[{built_in impl Sized for (u32, u64)}]; // anonymous local
269+
where _3 :< &'0 Either<(u32, u64)>[{built_in impl Sized for (u32, u64)}];
270270

271271
storage_live(_0)
272272
storage_live(_3)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::local_annotation, kind: Named(test_crate::local_annotation::'a, "'a") }
2+
--> tests/ui/closure-enclosing-regions.rs:10:16
3+
|
4+
10 | let y: &'a u32 = x;
5+
| ^^^^^^^
6+
7+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::local_annotation, kind: Named(test_crate::local_annotation::'a, "'a") }
8+
--> tests/ui/closure-enclosing-regions.rs:10:16
9+
|
10+
10 | let y: &'a u32 = x;
11+
| ^^^^^^^
12+
13+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::nested, kind: Named(test_crate::nested::'a, "'a") }
14+
--> tests/ui/closure-enclosing-regions.rs:26:20
15+
|
16+
26 | let y: &'a u32 = x;
17+
| ^^^^^^^
18+
19+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::nested, kind: Named(test_crate::nested::'a, "'a") }
20+
--> tests/ui/closure-enclosing-regions.rs:26:20
21+
|
22+
26 | let y: &'a u32 = x;
23+
| ^^^^^^^
24+
25+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::enclosing_closure::{closure#0}, kind: Named(test_crate::enclosing_closure::{closure#0}::'a, "'a") }
26+
--> tests/ui/closure-enclosing-regions.rs:37:20
27+
|
28+
37 | let y: &'a u32 = x;
29+
| ^^^^^^^
30+
31+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::enclosing_closure::{closure#0}, kind: Named(test_crate::enclosing_closure::{closure#0}::'a, "'a") }
32+
--> tests/ui/closure-enclosing-regions.rs:37:20
33+
|
34+
37 | let y: &'a u32 = x;
35+
| ^^^^^^^
36+
37+
ERROR Charon failed to translate this code (6 errors)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::local_annotation, kind: Named(test_crate::local_annotation::'a, "'a") }
2+
--> tests/ui/closure-enclosing-regions.rs:10:16
3+
|
4+
10 | let y: &'a u32 = x;
5+
| ^^^^^^^
6+
7+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::local_annotation, kind: Named(test_crate::local_annotation::'a, "'a") }
8+
--> tests/ui/closure-enclosing-regions.rs:10:16
9+
|
10+
10 | let y: &'a u32 = x;
11+
| ^^^^^^^
12+
13+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::no_capture, kind: Named(test_crate::no_capture::'a, "'a") }
14+
--> tests/ui/closure-enclosing-regions.rs:18:16
15+
|
16+
18 | let _: Option<&'a u32> = None;
17+
| ^^^^^^^^^^^^^^^
18+
19+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::no_capture, kind: Named(test_crate::no_capture::'a, "'a") }
20+
--> tests/ui/closure-enclosing-regions.rs:18:16
21+
|
22+
18 | let _: Option<&'a u32> = None;
23+
| ^^^^^^^^^^^^^^^
24+
25+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::nested, kind: Named(test_crate::nested::'a, "'a") }
26+
--> tests/ui/closure-enclosing-regions.rs:26:20
27+
|
28+
26 | let y: &'a u32 = x;
29+
| ^^^^^^^
30+
31+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::nested, kind: Named(test_crate::nested::'a, "'a") }
32+
--> tests/ui/closure-enclosing-regions.rs:26:20
33+
|
34+
26 | let y: &'a u32 = x;
35+
| ^^^^^^^
36+
37+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::enclosing_closure::{closure#0}, kind: Named(test_crate::enclosing_closure::{closure#0}::'a, "'a") }
38+
--> tests/ui/closure-enclosing-regions.rs:37:20
39+
|
40+
37 | let y: &'a u32 = x;
41+
| ^^^^^^^
42+
43+
error: Unexpected error: could not find the late-bound region variable LateParamRegion { scope: test_crate::enclosing_closure::{closure#0}, kind: Named(test_crate::enclosing_closure::{closure#0}::'a, "'a") }
44+
--> tests/ui/closure-enclosing-regions.rs:37:20
45+
|
46+
37 | let y: &'a u32 = x;
47+
| ^^^^^^^
48+
49+
ERROR Charon failed to translate this code (8 errors)

0 commit comments

Comments
 (0)