Skip to content

Commit

Permalink
checker: enhance err msg for unknown types for comptime $for (#20057)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Dec 3, 2023
1 parent 582ec6e commit f9f6ee7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions vlib/v/checker/comptime.v
Expand Up @@ -255,7 +255,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
typ := c.unwrap_generic(node.typ)
sym := c.table.final_sym(typ)
if sym.kind == .placeholder || typ.has_flag(.generic) {
c.error('unknown type `${sym.name}`', node.typ_pos)
c.error('\$for expects a type name to be used here, but ${sym.name} is not a type name',
node.typ_pos)
}
if node.kind == .fields {
if sym.kind in [.struct_, .interface_] {
Expand All @@ -268,7 +269,7 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
fields = sym.info.fields.clone()
}
else {
c.error('comptime field lookup supports only structs and interfaces currently, and ${sym.name} is neither',
c.error('iterating over .fields is supported only for structs and interfaces, and ${sym.name} is neither',
node.typ_pos)
return
}
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/checker/tests/comptime_for.out
Expand Up @@ -11,20 +11,20 @@ vlib/v/checker/tests/comptime_for.vv:3:7: warning: unused variable: `f`
| ^
4 | $for f in T.fields {
5 | $if f.typ is Huh {}
vlib/v/checker/tests/comptime_for.vv:2:12: error: unknown type `Huh`
vlib/v/checker/tests/comptime_for.vv:2:12: error: $for expects a type name to be used here, but Huh is not a type name
1 | fn unknown() {
2 | $for m in Huh.methods {}
| ~~~
3 | $for f in Huh.fields {}
4 | $for f in T.fields {
vlib/v/checker/tests/comptime_for.vv:3:12: error: unknown type `Huh`
vlib/v/checker/tests/comptime_for.vv:3:12: error: $for expects a type name to be used here, but Huh is not a type name
1 | fn unknown() {
2 | $for m in Huh.methods {}
3 | $for f in Huh.fields {}
| ~~~
4 | $for f in T.fields {
5 | $if f.typ is Huh {}
vlib/v/checker/tests/comptime_for.vv:4:12: error: unknown type `T`
vlib/v/checker/tests/comptime_for.vv:4:12: error: $for expects a type name to be used here, but T is not a type name
2 | $for m in Huh.methods {}
3 | $for f in Huh.fields {}
4 | $for f in T.fields {
Expand Down

0 comments on commit f9f6ee7

Please sign in to comment.