Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,7 @@ proc defaultNodeField(c: PContext, a: PNode, aTyp: PType, checkDefault: bool): P
else:
result = nil
of tyRange:
if c.graph.config.isDefined("nimPreviewRangeDefault"):
result = firstRange(c.config, aTypSkip)
else:
result = nil
result = firstRange(c.config, aTypSkip)
else:
result = nil

Expand Down
20 changes: 14 additions & 6 deletions compiler/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,18 @@ proc semPrivateAccess(c: PContext, n: PNode): PNode =
c.currentScope.allowPrivateAccess.add t.sym
result = newNodeIT(nkEmpty, n.info, getSysType(c.graph, n.info, tyVoid))

proc checkDefault(c: PContext, n: PNode): PNode =
proc checkDefault(c: PContext, n: PNode; isDefault: bool): PNode =
result = n
c.config.internalAssert result[1].typ.kind == tyTypeDesc
let constructed = result[1].typ.base
if constructed.requiresInit:
message(c.config, n.info, warnUnsafeDefault, typeToString(constructed))
# allows simple range types because they now have a valid default value
if constructed.requiresInit and constructed.skipTypes({tyGenericInst}).kind != tyRange:
# TODO: sorts out `nimPreviewRangeDefault` with `ranges` in the future
# TODO: be lenient with views and notnil types for now
if isDefault and c.config.features * {views, notnil} == {}:
localError(c.config, n.info, "The '$1' type doesn't have a valid default value" % typeToString(constructed))
else:
message(c.config, n.info, warnUnsafeDefault, typeToString(constructed))

proc magicsAfterOverloadResolution(c: PContext, n: PNode,
flags: TExprFlags; expectedType: PType = nil): PNode =
Expand Down Expand Up @@ -672,16 +678,18 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
let seqType = result[1].typ.skipTypes({tyPtr, tyRef, # in case we had auto-dereferencing
tyVar, tyGenericInst, tyOwned, tySink,
tyAlias, tyUserTypeClassInst})
if seqType.kind == tySequence and seqType.base.requiresInit:
if seqType.kind == tySequence and seqType.base.requiresInit and
seqType.base.skipTypes({tyGenericInst}).kind != tyRange:
# allows simple range types because they now have a valid default value
message(c.config, n.info, warnUnsafeSetLen, typeToString(seqType.base))
of mDefault:
result = checkDefault(c, n)
result = checkDefault(c, n, true)
let typ = result[^1].typ.skipTypes({tyTypeDesc})
let defaultExpr = defaultNodeField(c, result[^1], typ, false)
if defaultExpr != nil:
result = defaultExpr
of mZeroDefault:
result = checkDefault(c, n)
result = checkDefault(c, n, false)
of mIsolate:
if not checkIsolate(n[1]):
localError(c.config, n.info, "expression cannot be isolated: " & $n[1])
Expand Down
9 changes: 9 additions & 0 deletions tests/errmsgs/t25116.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
discard """
errormsg: "The 'RI' type doesn't have a valid default value"
"""


type RI {.requiresInit.} = object
v: int

var v = default(RI) # should be flagged as invalid
Loading