make the low bound the default value of ranges + ignoring views and notnil

This commit is contained in:
ringabout
2025-09-15 21:45:23 +08:00
parent 3aeea1f014
commit 8e7afb6bd6
2 changed files with 8 additions and 7 deletions

View File

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

View File

@@ -584,9 +584,11 @@ 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:
# 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
if isDefault:
# 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))
@@ -676,7 +678,9 @@ 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, true)