checker: check non-generic struct init (#11300)

pull/11311/head
yuyi 2021-08-25 19:40:40 +08:00 committed by GitHub
parent 50d1e9aa2b
commit f257a23313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -818,6 +818,10 @@ pub fn (mut c Checker) generic_insts_to_concrete() {
match parent.info { match parent.info {
ast.Struct { ast.Struct {
mut parent_info := parent.info as ast.Struct mut parent_info := parent.info as ast.Struct
if !parent_info.is_generic {
util.verror('generic error', 'struct `$parent.name` is not a generic struct, cannot instantiate to the concrete types')
continue
}
mut fields := parent_info.fields.clone() mut fields := parent_info.fields.clone()
if parent_info.generic_types.len == info.concrete_types.len { if parent_info.generic_types.len == info.concrete_types.len {
generic_names := parent_info.generic_types.map(c.table.get_type_symbol(it).name) generic_names := parent_info.generic_types.map(c.table.get_type_symbol(it).name)
@ -857,6 +861,10 @@ pub fn (mut c Checker) generic_insts_to_concrete() {
} }
ast.Interface { ast.Interface {
mut parent_info := parent.info as ast.Interface mut parent_info := parent.info as ast.Interface
if !parent_info.is_generic {
util.verror('generic error', 'interface `$parent.name` is not a generic interface, cannot instantiate to the concrete types')
continue
}
if parent_info.generic_types.len == info.concrete_types.len { if parent_info.generic_types.len == info.concrete_types.len {
mut fields := parent_info.fields.clone() mut fields := parent_info.fields.clone()
generic_names := parent_info.generic_types.map(c.table.get_type_symbol(it).name) generic_names := parent_info.generic_types.map(c.table.get_type_symbol(it).name)
@ -908,6 +916,10 @@ pub fn (mut c Checker) generic_insts_to_concrete() {
} }
ast.SumType { ast.SumType {
mut parent_info := parent.info as ast.SumType mut parent_info := parent.info as ast.SumType
if !parent_info.is_generic {
util.verror('generic error', 'sumtype `$parent.name` is not a generic sumtype, cannot instantiate to the concrete types')
continue
}
if parent_info.generic_types.len == info.concrete_types.len { if parent_info.generic_types.len == info.concrete_types.len {
mut fields := parent_info.fields.clone() mut fields := parent_info.fields.clone()
mut variants := parent_info.variants.clone() mut variants := parent_info.variants.clone()

View File

@ -0,0 +1 @@
generic error: struct `main.Test` is not a generic struct, cannot instantiate to the concrete types

View File

@ -0,0 +1,6 @@
struct Test {
}
fn main() {
println(Test<string>{})
}