Skip to content

Commit

Permalink
Fix pure classification of composite literals
Browse files Browse the repository at this point in the history
  • Loading branch information
jcp19 committed May 19, 2024
1 parent f78e491 commit 9699951
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ trait UnderlyingType { this: TypeInfoImpl =>
case value : PType => Some(value, this)
case _ => None
}
case st.AdtClause(_, typeDecl, _) => Some((typeDecl.right, this))
case _ => None // type not defined
}
case PDot(_, id) => entity(id) match {
case st.NamedType(decl, _, ctx) => inCtx(ctx, decl.right)
case st.TypeAlias(decl, _, ctx) => inCtx(ctx, decl.right)
case st.AdtClause(_, typeDecl, _) => Some((typeDecl.right, this))
case _ => None // type not defined
}
case t => Some((t, this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,14 @@ trait GhostExprTyping extends BaseTyping { this: TypeInfoImpl =>
case n: PIsComparable => asExpr(n.exp).forall(go)

case PCompositeLit(typ, _) => typ match {
case _: PArrayType | _: PImplicitSizeArrayType => !strong
case _ => true
case _: PImplicitSizeArrayType => true
case t: PType => underlyingTypeP(t) match {
case Some(ut) => ut match {
case _: PSliceType | _: PMapType => false
case _ => true
}
case None => false
}
}

case POptionNone(_) => true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package pkg
const N = 42

func foo() {
var a [N]int
assert len(a) == N
assert len(a) == 42
var a [N]int
assert len(a) == N
assert len(a) == 42
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ func test10() {
assert [4]int { 0, 1, 42, 8 }[2] == 42
assert forall i int :: 0 <= i && i < 4 ==> ([4]int { 0, 2, 4, 6 })[i] == i + i
}

pure
decreases
func test11() [3]int {
// pure functions may contain array literals
return [3]int{}
}
9 changes: 9 additions & 0 deletions src/test/resources/regressions/features/maps/maps-fail1.gobra
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ func test4() {
//:: ExpectedOutput(type_error)
_ = map[int]int {1: 2, 1: 3}
}

// error: expected pure expression without permissions, but got map[int]int { }
pure
decreases
func test5() map[int]int {
// pure functions may NOT contain map literals
//:: ExpectedOutput(type_error)
return map[int]int{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ package pkg
//:: ExpectedOutput(type_error)
requires s == t
func foo(s []int, t []int) {
}

// error: expected pure expression without permissions, but got []int { }
pure
decreases
func f() []int {
// pure functions may NOT contain slice literals
//:: ExpectedOutput(type_error)
return []int{}
}

0 comments on commit 9699951

Please sign in to comment.