Skip to content

Commit

Permalink
checker: prevent invalid SelectorExprs
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 committed Dec 20, 2023
1 parent aa8b93b commit 86a75b3
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _unreleased_
### Error and Type Checks
- If conditions must be of type bool
- Cannot assign from a void function call
- Prevent selecting fields of unsupported types (e.g. enums)
- Prevent some cases of identifier redefinition
- Any identifier by a function name
- Function argument by a variable
Expand Down
3 changes: 2 additions & 1 deletion lib/bait/checker/expr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ fun (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
return ast.VOID_TYPE
}

return ast.ANY_TYPE
c.error('cannot select from ${c.table.type_name(node.expr_type)}', node.pos)
return ast.PLACEHOLDER_TYPE
}

fun (c Checker) string_literal(node ast.StringLiteral) ast.Type {
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/preference/preference.bt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fun backend_from_string(s string) Backend {
'c' { .c }
else {
panic('Invalid backend: ${s}')
.js
Backend.js // TODO remove once @noreturn works properly
}
}
}
7 changes: 7 additions & 0 deletions tests/out/error/selector/cannot_select.in.bt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum Color {
red
green
}

c := Color.red
_ := c.my_field
1 change: 1 addition & 0 deletions tests/out/error/selector/cannot_select.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/out/error/selector/cannot_select.in.bt:7:7 error: cannot select from Color

0 comments on commit 86a75b3

Please sign in to comment.