fixes #20681; add efSkipFieldVisibilityCheck to skip check (#20639)

* don't sem const objectConstr defaults

* fixes

* add `efSkipFieldVisibilityCheck`; fixes nkBracket types

* fixes #20681

* fixes tests

* suggestion from @metagn

* fixes tests

Co-authored-by: xflywind <43030857+xflywind@users.noreply.github.com>
This commit is contained in:
ringabout 2022-10-29 04:19:40 +08:00 • committed by GitHub
commit 141abb7b75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 47 deletions

View file

@ -69,7 +69,8 @@ type
efWantStmt, efAllowStmt, efDetermineType, efExplain,
efWantValue, efOperand, efNoSemCheck,
efNoEvaluateGeneric, efInCall, efFromHlo, efNoSem2Check,
efNoUndeclared, efIsDotCall, efCannotBeDotCall
efNoUndeclared, efIsDotCall, efCannotBeDotCall,
efSkipFieldVisibilityCheck
# Use this if undeclared identifiers should not raise an error during
# overload resolution.

View file

@ -3075,7 +3075,8 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType
of paTupleFields: result = semTupleFieldsConstr(c, n, flags, expectedType)
of paSingle: result = semExpr(c, n[0], flags, expectedType)
of nkCurly: result = semSetConstr(c, n, expectedType)
of nkBracket: result = semArrayConstr(c, n, flags, expectedType)
of nkBracket:
result = semArrayConstr(c, n, flags, expectedType)
of nkObjConstr: result = semObjConstr(c, n, flags, expectedType)
of nkLambdaKinds: result = semProcAux(c, n, skProc, lambdaPragmas, flags)
of nkDerefExpr: result = semDeref(c, n)

View file

@ -76,7 +76,8 @@ proc semConstrField(c: PContext, flags: TExprFlags,
let assignment = locateFieldInInitExpr(c, field, initExpr)
if assignment != nil:
if nfSem in assignment.flags: return assignment[1]
if nfUseDefaultField in assignment[1].flags:
if nfUseDefaultField in assignment[1].flags or
efSkipFieldVisibilityCheck in flags:
discard
elif not fieldVisible(c, field):
localError(c.config, initExpr.info,
@ -415,7 +416,10 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
# field (if this is a case object, initialized fields in two different
# branches will be reported as an error):
var constrCtx = initConstrContext(t, result)
let (initResult, defaults) = semConstructTypeAux(c, constrCtx, flags)
let (initResult, defaults) = if nfUseDefaultField in n.flags:
semConstructTypeAux(c, constrCtx, flags + {efSkipFieldVisibilityCheck})
else:
semConstructTypeAux(c, constrCtx, flags)
result[0].sons.add defaults
var hasError = false # needed to split error detect/report for better msgs

View file

@ -220,6 +220,16 @@ proc isRecursiveType(t: PType, cycleDetector: var IntSet): bool =
else:
return false
proc fitDefaultNode(c: PContext, n: PNode): PType =
let expectedType = if n[^2].kind != nkEmpty: semTypeNode(c, n[^2], nil) else: nil
n[^1] = semConstExpr(c, n[^1], expectedType = expectedType)
if n[^2].kind != nkEmpty:
if expectedType != nil:
n[^1] = fitNodeConsiderViewType(c, expectedType, n[^1], n[^1].info)
result = n[^1].typ
else:
result = n[^1].typ
proc isRecursiveType*(t: PType): bool =
# handle simple recusive types before typeFinalPass
var cycleDetector = initIntSet()
@ -484,13 +494,7 @@ proc semTuple(c: PContext, n: PNode, prev: PType): PType =
checkMinSonsLen(a, 3, c.config)
var hasDefaultField = a[^1].kind != nkEmpty
if hasDefaultField:
a[^1] = semConstExpr(c, a[^1])
if a[^2].kind != nkEmpty:
typ = semTypeNode(c, a[^2], nil)
let def = semExprWithType(c, a[^1], {}, typ)
typ = fitNodeConsiderViewType(c, typ, def, def.info).typ
else:
typ = a[^1].typ
typ = fitDefaultNode(c, a)
elif a[^2].kind != nkEmpty:
typ = semTypeNode(c, a[^2], nil)
if c.graph.config.isDefined("nimPreviewRangeDefault") and typ.skipTypes(abstractInst).kind == tyRange:
@ -824,13 +828,7 @@ proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
var typ: PType
var hasDefaultField = n[^1].kind != nkEmpty
if hasDefaultField:
n[^1] = semConstExpr(c, n[^1])
if n[^2].kind != nkEmpty:
typ = semTypeNode(c, n[^2], nil)
let def = semExprWithType(c, n[^1], {}, typ)
typ = fitNodeConsiderViewType(c, typ, def, def.info).typ
else:
typ = n[^1].typ
typ = fitDefaultNode(c, n)
propagateToOwner(rectype, typ)
elif n[^2].kind == nkEmpty:
localError(c.config, n.info, errTypeExpected)