mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
Fix crash when using uninstantiated generic (#22379)
* Add test case * Add in a bounds check when accessing generic types Removes idnex out of bounds exception when comparing a generic that isn't fully instantiated
This commit is contained in:
@@ -1536,6 +1536,8 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
|
||||
if x.kind == tyGenericInvocation:
|
||||
if f[0] == x[0]:
|
||||
for i in 1..<f.len:
|
||||
# Handle when checking against a generic that isn't fully instantiated
|
||||
if i >= x.len: return
|
||||
let tr = typeRel(c, f[i], x[i], flags)
|
||||
if tr <= isSubtype: return
|
||||
result = isGeneric
|
||||
|
||||
16
tests/generics/tuninstantiated_failure.nim
Normal file
16
tests/generics/tuninstantiated_failure.nim
Normal file
@@ -0,0 +1,16 @@
|
||||
discard """
|
||||
cmd: "nim check $file"
|
||||
"""
|
||||
|
||||
type
|
||||
Test[T, K] = object
|
||||
name: string
|
||||
Something = Test[int]
|
||||
|
||||
func `[]`[T, K](x: var Test[T, K], idx: int): var Test[T, K] =
|
||||
x
|
||||
|
||||
var b: Something
|
||||
# Should give a type-mismatch since Something isn't a valid Test
|
||||
b[0].name = "Test" #[tt.Error
|
||||
^ type mismatch]#
|
||||
Reference in New Issue
Block a user