Skip to content

Commit b7182db

Browse files
authored
Merge pull request #1600 from Nadrieril/fix-annot-ice
Fix an ICE by normalizing opaques
2 parents d147e65 + fcb9f5f commit b7182db

4 files changed

Lines changed: 243 additions & 15 deletions

File tree

‎charon/src/bin/charon-driver/hax/rustc_utils.rs‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,27 @@ pub fn translate_user_annotation<'tcx>(
5353
// Compute the inferred type of the place, and constraints needed to match it with the
5454
// user-provided type.
5555
let (projected_tys, constraints): (Vec<ty::Ty<'tcx>>, QueryRegionConstraints<'tcx>) = {
56-
let mut constraints = param_env
56+
let mut all_constraints: Vec<Option<&'tcx QueryRegionConstraints<'tcx>>> = Vec::new();
57+
58+
// `AscribeUserType` needs a normalized type.
59+
let normalized = param_env
60+
.and(Normalize {
61+
value: ty::set_opaques_to_non_rigid(tcx, inferred_ty),
62+
})
63+
.fully_perform(&infcx, owner, span)
64+
.map_err(|_| "could not normalize inferred annotation type".to_owned())?;
65+
let inferred_ty = normalized.output;
66+
all_constraints.push(normalized.constraints);
67+
68+
let ascribe_result = param_env
5769
.and(AscribeUserType {
5870
mir_ty: inferred_ty,
5971
user_ty,
6072
})
6173
.fully_perform(&infcx, owner, span)
62-
.map_err(|_| "could not recover user type ascription".to_owned())?
63-
.constraints
64-
.cloned()
65-
.unwrap_or_default();
66-
// Add any normalization obligations coming from projections. In particular, projecting a field
67-
// can expose an associated type with lifetime requirements.
74+
.map_err(|_| "could not recover user type ascription".to_owned())?;
75+
all_constraints.push(ascribe_result.constraints);
76+
6877
let mut projected_tys = Vec::new();
6978
for projections in projections {
7079
let mut projected_ty = mir::PlaceTy::from_ty(inferred_ty);
@@ -77,14 +86,7 @@ pub fn translate_user_annotation<'tcx>(
7786
.fully_perform(&infcx, owner, span)
7887
.map_err(|_| "could not normalize user type projection".to_owned())?;
7988
projected_ty.ty = resolved.output;
80-
if let Some(extra) = resolved.constraints {
81-
constraints
82-
.constraints
83-
.extend(extra.constraints.iter().copied());
84-
constraints
85-
.assumptions
86-
.extend(extra.assumptions.iter().copied());
87-
}
89+
all_constraints.push(resolved.constraints);
8890
let Some(projection) = iter.next() else { break };
8991
if matches!(
9092
projection,
@@ -104,6 +106,18 @@ pub fn translate_user_annotation<'tcx>(
104106
}
105107
projected_tys.push(projected_ty.ty);
106108
}
109+
110+
// Merge the constraints we got from normalizing, relating and projecting.
111+
let mut constraints: QueryRegionConstraints<'tcx> = Default::default();
112+
for c in all_constraints.into_iter().flatten() {
113+
constraints
114+
.constraints
115+
.extend(c.constraints.iter().copied());
116+
constraints
117+
.assumptions
118+
.extend(c.assumptions.iter().copied());
119+
}
120+
107121
(projected_tys, constraints)
108122
};
109123

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Final LLBC before serialization:
2+
3+
// Full name: core::marker::MetaSized
4+
#[fundamental]
5+
#[lang_item(MetaSized)]
6+
pub trait MetaSized<Self>
7+
8+
// Full name: core::marker::Sized
9+
#[fundamental]
10+
#[lang_item(Sized)]
11+
pub trait Sized<Self>
12+
{
13+
proof ImpliedClause0: (Self: MetaSized)
14+
non-dyn-compatible
15+
}
16+
17+
// Full name: core::option::Option
18+
#[lang_item(Option)]
19+
#[diagnostic_item("Option")]
20+
pub enum Option<T>
21+
where
22+
TraitClause0: (T: Sized),
23+
{
24+
None,
25+
Some { _0: T },
26+
}
27+
28+
struct () {}
29+
30+
struct (_, _)<A, B>
31+
where
32+
TraitClause0: (A: Sized),
33+
{
34+
_0: A,
35+
_1: B,
36+
}
37+
38+
// Full name: test_crate::destructure
39+
pub fn destructure<'a>(x_1: &'a u32) -> &'a u32
40+
{
41+
let mut _0: &'13 u32; // return
42+
let x_1: &'14 u32; // arg #1
43+
let x_2: &'a u32; // local
44+
let mut _3: (&'22 u32, u32); // anonymous local
45+
where _3 :< (&'a u32, u32);
46+
let mut _4: &'26 u32; // anonymous local
47+
48+
storage_live(_0)
49+
storage_live(_3)
50+
storage_live(_4)
51+
_4 = copy x_1
52+
_3 = (move _4, const 0u32)
53+
storage_dead(_4)
54+
storage_live(x_2)
55+
x_2 = copy _3._0
56+
storage_dead(_3)
57+
_0 = &(*x_2)
58+
storage_dead(x_2)
59+
storage_dead(x_1)
60+
return
61+
}
62+
63+
// Full name: test_crate::destructure::Pair
64+
type Pair<'a> = (&'a u32, u32)
65+
66+
// Full name: test_crate::Opaque
67+
type Opaque = u32
68+
69+
// Full name: test_crate::define
70+
fn define() -> u32
71+
{
72+
let mut _0: u32; // return
73+
74+
storage_live(_0)
75+
_0 = const 0u32
76+
return
77+
}
78+
79+
// Full name: test_crate::construct
80+
pub fn construct()
81+
{
82+
let mut _0: (); // return
83+
let mut _1: Option<u32>[{built_in impl Sized for u32}]; // anonymous local
84+
85+
storage_live(_0)
86+
_0 = ()
87+
storage_live(_1)
88+
_1 = Option::None { }
89+
storage_dead(_1)
90+
_0 = ()
91+
return
92+
}
93+
94+
95+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Final LLBC before serialization:
2+
3+
// Full name: core::marker::MetaSized
4+
#[fundamental]
5+
#[lang_item(MetaSized)]
6+
pub trait MetaSized<Self>
7+
8+
// Full name: core::marker::Sized
9+
#[fundamental]
10+
#[lang_item(Sized)]
11+
pub trait Sized<Self>
12+
{
13+
proof ImpliedClause0: (Self: MetaSized)
14+
non-dyn-compatible
15+
}
16+
17+
// Full name: core::option::Option
18+
#[lang_item(Option)]
19+
#[diagnostic_item("Option")]
20+
pub enum Option<T>
21+
where
22+
TraitClause0: (T: Sized),
23+
{
24+
None,
25+
Some { _0: T },
26+
}
27+
28+
struct () {}
29+
30+
struct (_, _)<A, B>
31+
where
32+
TraitClause0: (A: Sized),
33+
{
34+
_0: A,
35+
_1: B,
36+
}
37+
38+
// Full name: test_crate::destructure
39+
pub fn destructure<'a>(x_1: &'a u32) -> &'a u32
40+
{
41+
let mut _0: &'13 u32; // return
42+
let x_1: &'14 u32; // arg #1
43+
let x_2: &'a u32; // local
44+
let mut _3: (&'22 u32, u32); // anonymous local
45+
where _3 :< (&'a u32, u32);
46+
let mut _4: &'26 u32; // anonymous local
47+
48+
storage_live(_0)
49+
storage_live(_3)
50+
storage_live(_4)
51+
_4 = copy x_1
52+
_3 = (move _4, const 0u32)
53+
storage_dead(_4)
54+
storage_live(x_2)
55+
x_2 = copy _3._0
56+
storage_dead(_3)
57+
_0 = &(*x_2)
58+
storage_dead(x_2)
59+
storage_dead(x_1)
60+
return
61+
}
62+
63+
// Full name: test_crate::destructure::Pair
64+
type Pair<'a> = (&'a u32, u32)
65+
66+
// Full name: test_crate::Opaque
67+
type Opaque = u32
68+
69+
// Full name: test_crate::define
70+
fn define() -> u32
71+
{
72+
let mut _0: u32; // return
73+
74+
storage_live(_0)
75+
_0 = const 0u32
76+
return
77+
}
78+
79+
// Full name: test_crate::construct
80+
pub fn construct()
81+
{
82+
let mut _0: (); // return
83+
let mut _1: Option<u32>[{built_in impl Sized for u32}]; // anonymous local
84+
85+
storage_live(_0)
86+
_0 = ()
87+
storage_live(_1)
88+
_1 = Option::None { }
89+
storage_dead(_1)
90+
_0 = ()
91+
return
92+
}
93+
94+
95+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ revisions=current,next
2+
//@ charon-args=--mir promoted
3+
//@[current] rustc-args=-Znext-solver=coherence
4+
//@[next] rustc-args=-Znext-solver=globally
5+
#![feature(type_alias_impl_trait)]
6+
// Type annotations can contain opaques, which used to cause an ICE in our annotation recovery
7+
// logic.
8+
9+
pub fn destructure<'a>(x: &'a u32) -> &'a u32 {
10+
type Pair<'a> = impl Sized + 'a;
11+
let (x, _): Pair<'a> = (x, 0u32);
12+
x
13+
}
14+
15+
type Opaque = impl Sized;
16+
17+
#[define_opaque(Opaque)]
18+
fn define() -> Opaque {
19+
0u32
20+
}
21+
22+
pub fn construct() {
23+
let _ = None::<Opaque>;
24+
}

0 commit comments

Comments
 (0)