Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stricter and more correct referenceability checks #979

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion source/rock/middle/ArrayAccess.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,20 @@ ArrayAccess: class extends Expression {
b toString()
}

isReferencable: func -> Bool { true }
isReferencable: func -> Bool {
if (array getType()) {
if (array getType() instanceOf?(ArrayType)) {
// ArrayTypes with expressions are actually C arrays, which are referencable (while ooc arrays are not)
arrayType := array getType() as ArrayType

return arrayType expr != null
} else if (array getType() instanceOf?(PointerType)) {
return true
}
}

array isReferencable()
}

replace: func (oldie, kiddo: Node) -> Bool {
match oldie {
Expand Down
11 changes: 9 additions & 2 deletions source/rock/middle/VariableAccess.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,15 @@ VariableAccess: class extends Expression {
expr ? (expr toString() + " " + prettyName) : prettyName
}

isReferencable: func -> Bool { ref && ref instanceOf?(VariableDecl) &&
(ref as VariableDecl isExtern() && ref as VariableDecl isConst()) ? false : true }
isReferencable: func -> Bool {
if (ref && ref instanceOf?(VariableDecl)) {
if (ref as VariableDecl isExtern() && ref as VariableDecl isConst()) {
return false
}
}

true
}

replace: func (oldie, kiddo: Node) -> Bool {
match oldie {
Expand Down
11 changes: 11 additions & 0 deletions test/compiler/arrays/array-retrieve-calculated-property.ooc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Foo: cover {
calculated ::= 42
}

describe("should be able to directly use a calculated property of a cover stored in an ooc array", ||
arr := Foo[1] new()
foo: Foo
arr[0] = foo

expect(42, arr[0] calculated)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import structs/ArrayList

Foos: class {
init: func
operator [] (index: Int) -> Foo {
Foo new(42)
}
}

Foo: cover {
bar: Int
init: func@ (=bar)
}

describe("we should be able to call a generic function on a calculated property of a cover returned by an operator call", ||
foos := Foos new()
list := ArrayList<Int> new()

list add(foos[0] bar)

expect(42, list last())
)