From 039f57ec4bff170ca0379b638623e2a132342fe8 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 23 Oct 2020 10:33:41 +0200 Subject: [PATCH] fixes #15533 [backport:1.4] --- compiler/sigmatch.nim | 10 +++++++--- tests/views/treject_var_inside_option.nim | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 tests/views/treject_var_inside_option.nim diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 687634e932..2b5b70a0b8 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -92,6 +92,7 @@ type isSubtype, isSubrange, # subrange of the wanted type; no type conversion # but apart from that counts as ``isSubtype`` + isVarConvertible, isBothMetaConvertible # generic proc parameter was matched against # generic type, e.g., map(mySeq, x=>x+1), # maybe recoverable by rerun if the parameter is @@ -1191,7 +1192,10 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, of tyFloat128: result = handleFloatRange(f, a) of tyVar, tyLent: if aOrig.kind == f.kind: result = typeRel(c, f.base, aOrig.base, flags) - else: result = typeRel(c, f.base, aOrig, flags + {trNoCovariance}) + else: + result = typeRel(c, f.base, aOrig, flags + {trNoCovariance}) + if result > isVarConvertible: + result = isVarConvertible subtypeCheck() of tyArray: case a.kind @@ -1976,7 +1980,7 @@ proc incMatches(m: var TCandidate; r: TTypeRelation; convMatch = 1) = of isFromIntLit: inc(m.intConvMatches, 256) of isInferredConvertible: inc(m.convMatches) - of isEqual: inc(m.exactMatches) + of isEqual, isVarConvertible: inc(m.exactMatches) of isNone: discard template matchesVoidProc(t: PType): bool = @@ -2126,7 +2130,7 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType, # ``isIntConv`` and ``isIntLit`` here: inc(m.intConvMatches, 256) result = implicitConv(nkHiddenStdConv, f, arg, m, c) - of isEqual: + of isEqual, isVarConvertible: inc(m.exactMatches) result = arg if skipTypes(f, abstractVar-{tyTypeDesc}).kind == tyTuple or diff --git a/tests/views/treject_var_inside_option.nim b/tests/views/treject_var_inside_option.nim new file mode 100644 index 0000000000..e441fda7e1 --- /dev/null +++ b/tests/views/treject_var_inside_option.nim @@ -0,0 +1,12 @@ +discard """ + errormsg: "type mismatch: got but expected 'Option[var int]'" + cmd: "nim check $file" + line: 12 +""" + +# bug #15533 + +{.experimental: "views".} +import options +var a = [1, 2, 3, 4] +let o: Option[var int] = some(a[0])