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:
Jake Leahy
2023-08-04 20:21:36 +10:00
committed by GitHub
parent 7c2a2c8dc8
commit 3efabd3ec6
2 changed files with 18 additions and 0 deletions

View File

@@ -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

View 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]#