checker: fix too strict checking with generics in assignment type mismatch (fix #20335) (#20346)

This commit is contained in:
shove 2024-01-03 01:53:12 +08:00 committed by GitHub
parent 075d17d66f
commit 7a56ebf23b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -735,7 +735,9 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
}
}
}
if !is_blank_ident && right_sym.kind != .placeholder && left_sym.kind != .interface_ {
if !is_blank_ident && right_sym.kind != .placeholder && left_sym.kind != .interface_
&& ((!right_type.has_flag(.generic) && !left_type.has_flag(.generic))
|| right_sym.kind != left_sym.kind) {
// Dual sides check (compatibility check)
c.check_expected(right_type_unwrapped, left_type_unwrapped) or {
// allow literal values to auto deref var (e.g.`for mut v in values { v = 1.0 }`)

View File

@ -0,0 +1,33 @@
module main
pub type EventListener[T] = fn (T) !
pub type Check[T] = fn (T) bool
pub struct EventController[T] {
mut:
id int
listeners map[int]EventListener[T]
}
fn (mut ec EventController[T]) generate_id() int {
return ec.id++
}
pub fn (mut ec EventController[T]) override(listener EventListener[T]) EventController[T] {
ec.listeners = {
ec.generate_id(): listener
}
return ec
}
fn use[T](_ EventController[T]) {}
struct Foo {}
struct Bar {}
fn test_main() {
use(EventController[Foo]{})
use(EventController[Bar]{})
}