Skip to content

Commit

Permalink
Add forgoten test file
Browse files Browse the repository at this point in the history
  • Loading branch information
nilehmann committed Oct 15, 2023
1 parent 67ebbb6 commit 0946857
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
31 changes: 18 additions & 13 deletions crates/flux-refineck/src/type_env/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl FallibleTypeFolder for Unfolder<'_, '_, '_> {
let Some(elem) = self.cursor.next() else {
return self.unfold(ty);
};
let ty = self.rcx.unpack_with(ty, UnpackFlags::SHALLOW);
let ty = self.unpack(ty);
match elem {
PlaceElem::Deref => self.deref(&ty),
PlaceElem::Field(f) => self.field(&ty, f),
Expand Down Expand Up @@ -413,11 +413,11 @@ impl<'a, 'rcx, 'tcx> Unfolder<'a, 'rcx, 'tcx> {
Ok(Ty::ptr(PtrKind::Box, Path::from(loc)))
}
} else if ty.is_struct() {
let ty = self.rcx.unpack_with(ty, UnpackFlags::SHALLOW);
let ty = self.unpack(ty);
let ty = self.downcast(&ty, FIRST_VARIANT)?;
Ok(ty)
} else if ty.is_array() || ty.is_slice() {
Ok(self.rcx.unpack_with(ty, UnpackFlags::SHALLOW))
Ok(self.unpack(ty))
} else {
Ok(ty.clone())
}
Expand Down Expand Up @@ -478,7 +478,7 @@ impl<'a, 'rcx, 'tcx> Unfolder<'a, 'rcx, 'tcx> {
let mut fields = downcast_struct(self.genv, adt, args, idx)?
.into_iter()
.map(|ty| {
let ty = self.rcx.unpack_with(&ty, self.unpack_flags_for_downcast());
let ty = self.unpack_for_downcast(&ty);
self.assume_invariants(&ty);
ty
})
Expand All @@ -503,7 +503,7 @@ impl<'a, 'rcx, 'tcx> Unfolder<'a, 'rcx, 'tcx> {
let fields = downcast(self.genv, self.rcx, adt, args, variant, idx)?
.into_iter()
.map(|ty| {
let ty = self.rcx.unpack_with(&ty, self.unpack_flags_for_downcast());
let ty = self.unpack_for_downcast(&ty);
self.assume_invariants(&ty);
ty
})
Expand Down Expand Up @@ -532,6 +532,19 @@ impl<'a, 'rcx, 'tcx> Unfolder<'a, 'rcx, 'tcx> {
Ok(())
}

fn unpack(&mut self, ty: &Ty) -> Ty {
self.rcx.unpack_with(ty, UnpackFlags::SHALLOW)
}

fn unpack_for_downcast(&mut self, ty: &Ty) -> Ty {
let flags = if self.in_ref == Some(Mutability::Mut) {
UnpackFlags::NO_UNPACK_EXISTS | UnpackFlags::SHALLOW
} else {
UnpackFlags::SHALLOW
};
self.rcx.unpack_with(ty, flags)
}

fn assume_invariants(&mut self, ty: &Ty) {
self.rcx
.assume_invariants(ty, self.checker_conf.check_overflow);
Expand All @@ -542,14 +555,6 @@ impl<'a, 'rcx, 'tcx> Unfolder<'a, 'rcx, 'tcx> {
self.cursor.change_root(path);
}

fn unpack_flags_for_downcast(&self) -> UnpackFlags {
if self.in_ref == Some(Mutability::Mut) {
UnpackFlags::NO_UNPACK_EXISTS
} else {
UnpackFlags::empty()
}
}

fn should_continue(&mut self) -> bool {
if self.has_work {
self.has_work = false;
Expand Down
14 changes: 14 additions & 0 deletions crates/flux-tests/tests/neg/surface/invariant00.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Check that we don't assume invariants under a generic type argument.
struct S<T> {
x: T,
}

// We don't assume `n >= 0` from the `u32[n]` inside `S`, that would be unsound in general.
#[flux::sig(fn(i32[@n], S<u32[n]>))]
fn test(x: i32, _: S<u32>) {
assert(x >= 0); //~ERROR refinement type error
}

#[flux::sig(fn(bool[true]))]
fn assert(_: bool) {}

0 comments on commit 0946857

Please sign in to comment.