Merge pull request 'fix-nil-void-type' (#36) from fix-nil-void-type into main

Reviewed-on: sashakoshka/fspl#36
This commit is contained in:
Sasha Koshka 2024-02-12 19:49:57 +00:00
commit b44e4f3f9d
5 changed files with 62 additions and 20 deletions

View File

@ -36,15 +36,19 @@ func (this *Tree) canAssign (
fail := func () error {
switch mode {
case strict:
return errors.Errorf(pos, "expected %v", destination)
return errors.Errorf (
pos, "expected %v",
entity.FormatType(destination))
case weak:
return errors.Errorf (
pos, "cannot use %v as %v",
source, destination)
entity.FormatType(source),
entity.FormatType(destination))
case structural:
return errors.Errorf (
pos, "cannot convert from %v to %v",
source, destination)
entity.FormatType(source),
entity.FormatType(destination))
default:
panic(fmt.Sprint (
"BUG: analyzer doesnt know about mode", mode))
@ -152,7 +156,8 @@ func (this *Tree) canAssignCoerce (
fail := func () error {
return errors.Errorf (
pos, "cannot convert from %v to %v",
source, destination)
entity.FormatType(source),
entity.FormatType(destination))
}
destination = ReduceToBase(destination)
@ -222,7 +227,9 @@ func (this *Tree) canAssignSliceArray (
) error {
err := this.canAssign(pos, destination.Element, strict, source.Element)
if err != nil {
return errors.Errorf(pos, "expected %v", destination)
return errors.Errorf (
pos, "expected %v",
entity.FormatType(destination))
} else {
return nil
}

View File

@ -378,7 +378,9 @@ func (this *Tree) analyzeOperation (
error,
) {
wrongInto := func () (entity.Expression, error) {
return nil, errors.Errorf(operation.Position, "expected %v", into)
return nil, errors.Errorf (
operation.Position, "expected %v",
entity.FormatType(into))
}
wrongArgCount := func () (entity.Expression, error) {
@ -578,18 +580,21 @@ func (this *Tree) analyzeMemberAccess (
referenced, ok := ReduceToBase(sourceTypeAny.Referenced).(*entity.TypeStruct)
if !ok {
return nil, errors.Errorf (
access.Position, "cannot access members of %v", source)
access.Position, "cannot access members of %v",
source)
}
sourceType = referenced
default:
return nil, errors.Errorf (
access.Position, "cannot access members of %v", source)
access.Position, "cannot access members of %v",
source)
}
member, ok := sourceType.MemberMap[access.Member]
if !ok {
return nil, errors.Errorf (
access.Position, "no member %v", access)
access.Position, "no member %v",
access)
}
err = this.canAssign(access.Position, into, mode, member.Type())
@ -665,7 +670,9 @@ func (this *Tree) analyzeBreak (
error,
) {
if into != nil {
return nil, errors.Errorf(brk.Position, "expected %v", into)
return nil, errors.Errorf (
brk.Position, "expected %v",
entity.FormatType(into))
}
loop, ok := this.topLoop()
@ -700,7 +707,9 @@ func (this *Tree) analyzeReturn (
error,
) {
if into != nil {
return nil, errors.Errorf(ret.Position, "expected %v", into)
return nil, errors.Errorf (
ret.Position, "expected %v",
entity.FormatType(into))
}
ret.Declaration, _ = this.topDeclaration()

View File

@ -15,13 +15,13 @@ func (this *Tree) analyzeLiteralInt (
if !isNumeric(into) {
return nil, errors.Errorf (
literal.Position, "cannot use integer literal as %v",
into)
entity.FormatType(into))
}
if isInteger(into) && !inRange(into, int64(literal.Value)) {
return nil, errors.Errorf (
literal.Position, "integer literal out of range for type %v",
into)
entity.FormatType(into))
}
literal.Ty = into
@ -39,7 +39,7 @@ func (this *Tree) analyzeLiteralFloat (
if !isFloat(into) {
return nil, errors.Errorf (
literal.Position, "cannot use float literal as %v",
into)
entity.FormatType(into))
}
literal.Ty = into
@ -59,7 +59,7 @@ func (this *Tree) analyzeLiteralString (
errCantUse := func () error {
return errors.Errorf (
literal.Position, "cannot use string literal as %v",
into)
entity.FormatType(into))
}
fillUTF32 := func () {
@ -86,13 +86,13 @@ func (this *Tree) analyzeLiteralString (
literal.Position,
"cannot fit string literal of length " +
"%v into %v",
length, into)
length, entity.FormatType(into))
} else if length < 1 {
return nil, errors.Errorf (
literal.Position,
"string literal must have data when " +
"assigning to %v",
into)
entity.FormatType(into))
}
case *entity.TypeArray:
element := ReduceToBase(base.Element)
@ -176,7 +176,7 @@ func (this *Tree) analyzeLiteralArray (
default:
return nil, errors.Errorf (
literal.Position, "cannot use array literal as %v",
into)
entity.FormatType(into))
}
for index, element := range literal.Elements {
@ -201,7 +201,7 @@ func (this *Tree) analyzeLiteralStruct (
if !ok {
return nil, errors.Errorf (
literal.Position, "cannot use struct literal as %v",
into)
entity.FormatType(into))
}
literal, err := this.assembleStructLiteralMap(literal)
@ -235,7 +235,7 @@ func (this *Tree) analyzeLiteralBoolean (
if !isBoolean(into) {
return nil, errors.Errorf (
literal.Position, "cannot use boolean literal as %v",
into)
entity.FormatType(into))
}
literal.Ty = into

View File

@ -162,3 +162,19 @@ testString (test,
Bird: (~ [fly] [land])
`)
}
func TestTypeIntegerLiteralVoid (test *testing.T) {
testStringErr (test,
"cannot use integer literal as Void", 2, 10,
`
[main] = 5
`)
}
func TestTypeStringLiteralVoid (test *testing.T) {
testStringErr (test,
"cannot use string literal as Void", 2, 10,
`
[main] = 'hello'
`)
}

View File

@ -208,3 +208,13 @@ func TypesEqual (left, right Type) bool {
if left == nil { return true }
return left.Equals(right)
}
// FormatType returns a string representing a type. If the type is nil, it
// returns "Void".
func FormatType (ty Type) string {
if ty == nil {
return "Void"
} else {
return ty.String()
}
}