Skip to content

Commit f3337e5

Browse files
committed
Turn some of the errors back into warnings
1 parent 14006fa commit f3337e5

File tree

7 files changed

+62
-33
lines changed

7 files changed

+62
-33
lines changed

compiler/lineinfos.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type
3737
warnUnusedImportX,
3838
warnInheritFromException,
3939
warnEachIdentIsTuple,
40+
warnUnsafeSetLen,
41+
warnUnsafeDefault,
4042
warnProveInit, warnProveField, warnProveIndex,
4143
warnStaticIndexCheck, warnGcUnsafe, warnGcUnsafe2,
4244
warnUninit, warnGcMem, warnDestructor, warnLockLevel, warnResultShadowed,
@@ -87,6 +89,9 @@ const
8789
warnUnusedImportX: "imported and not used: '$1'",
8890
warnInheritFromException: "inherit from a more precise exception type like ValueError, IOError or OSError",
8991
warnEachIdentIsTuple: "each identifier is a tuple",
92+
warnUnsafeSetLen: "setLen can potentially expand the sequence, " &
93+
"but the element type '$1' doesn't have a valid default value",
94+
warnUnsafeDefault: "The '$1' type doesn't have a valid default value",
9095
warnProveInit: "Cannot prove that '$1' is initialized. This will become a compile time error in the future.",
9196
warnProveField: "cannot prove that field '$1' is accessible",
9297
warnProveIndex: "cannot prove index '$1' is valid",
@@ -146,6 +151,7 @@ const
146151
"TypelessParam", "UseBase", "WriteToForeignHeap",
147152
"UnsafeCode", "UnusedImport", "InheritFromException",
148153
"EachIdentIsTuple",
154+
"UnsafeSetLen", "UnsafeDefault",
149155
"ProveInit", "ProveField", "ProveIndex",
150156
"IndexCheck", "GcUnsafe", "GcUnsafe2", "Uninit",
151157
"GcMem", "Destructor", "LockLevel", "ResultShadowed",

compiler/semexprs.nim

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,20 +2264,6 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
22642264
of mSizeOf:
22652265
markUsed(c, n.info, s)
22662266
result = semSizeof(c, setMs(n, s))
2267-
of mSetLengthSeq:
2268-
result = semDirectOp(c, n, flags)
2269-
let seqType = result[1].typ.skipTypes({tyPtr, tyRef, # in case we had auto-dereferencing
2270-
tyVar, tyGenericInst, tyOwned, tySink,
2271-
tyAlias, tyUserTypeClassInst})
2272-
if seqType.kind == tySequence and seqType.base.requiresInit:
2273-
localError(c.config, n.info, "setLen can potentially expand the sequence, " &
2274-
"but the element type $1 doesn't have a default value.",
2275-
[typeToString(seqType.base)])
2276-
of mDefault:
2277-
result = semDirectOp(c, n, flags)
2278-
c.config.internalAssert result[1].typ.kind == tyTypeDesc
2279-
if result[1].typ.base.requiresInit:
2280-
localError(c.config, n.info, "not nil types don't have a default value")
22812267
else:
22822268
result = semDirectOp(c, n, flags)
22832269

compiler/semmagic.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,18 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
505505
result[0] = newSymNode(t.destructor)
506506
of mUnown:
507507
result = semUnown(c, n)
508-
else: result = n
508+
of mSetLengthSeq:
509+
result = n
510+
let seqType = result[1].typ.skipTypes({tyPtr, tyRef, # in case we had auto-dereferencing
511+
tyVar, tyGenericInst, tyOwned, tySink,
512+
tyAlias, tyUserTypeClassInst})
513+
if seqType.kind == tySequence and seqType.base.requiresInit:
514+
message(c.config, n.info, warnUnsafeSetLen, typeToString(seqType.base))
515+
of mDefault:
516+
result = n
517+
c.config.internalAssert result[1].typ.kind == tyTypeDesc
518+
let constructed = result[1].typ.base
519+
if constructed.requiresInit:
520+
message(c.config, n.info, warnUnsafeDefault, typeToString(constructed))
521+
else:
522+
result = n

compiler/semobjconstr.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ proc semConstructTypeAux(c: PContext,
336336
let base = t[0]
337337
if base == nil: break
338338
t = skipTypes(base, skipPtrs)
339+
if t.kind == tyGenericParam:
340+
# XXX: This is not supposed to happen, but apparently
341+
# there are some issues in semtypinst. Luckily, it
342+
# seems to affect only `computeRequiresInit`.
343+
return
339344
constrCtx.needsFullInit = constrCtx.needsFullInit or
340345
tfNeedsFullInit in t.flags
341346

compiler/sempass2.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ proc useVar(a: PEffects, n: PNode) =
254254
a.init.add s.id
255255
elif s.id notin a.init:
256256
if s.typ.requiresInit:
257-
localError(a.config, n.info, errProveInit, s.name.s)
257+
message(a.config, n.info, warnProveInit, s.name.s)
258258
else:
259259
message(a.config, n.info, warnUninit, s.name.s)
260260
# prevent superfluous warnings about the same variable:
@@ -795,7 +795,7 @@ proc track(tracked: PEffects, n: PNode) =
795795
# var s: seq[notnil]; newSeq(s, 0) is a special case!
796796
discard
797797
else:
798-
localError(tracked.config, arg.info, errProveInit, $arg)
798+
message(tracked.config, arg.info, warnProveInit, $arg)
799799

800800
# check required for 'nim check':
801801
if n[1].typ.len > 0:
@@ -1140,7 +1140,7 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) =
11401140
s.kind in {skProc, skFunc, skConverter, skMethod}:
11411141
var res = s.ast[resultPos].sym # get result symbol
11421142
if res.id notin t.init:
1143-
localError(g.config, body.info, errProveInit, "result")
1143+
message(g.config, body.info, warnProveInit, "result")
11441144
let p = s.ast[pragmasPos]
11451145
let raisesSpec = effectSpec(p, wRaises)
11461146
if not isNil(raisesSpec):

tests/constructors/tinvalid_construction.nim

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ reject THasNotNils(a: notNilRef, b: nilRef, c: nilRef) # `b` shouldn't be n
113113
reject THasNotNils(b: notNilRef, c: notNilRef) # there is a missing not nil field
114114
reject THasNotNils() # again, missing fields
115115
accept THasNotNils(a: notNilRef, b: notNilRef) # it's OK to omit a non-mandatory field
116-
reject default(THasNotNils)
117-
reject userDefinedDefault(THasNotNils)
116+
# produces only warning: reject default(THasNotNils)
117+
# produces only warning: reject userDefinedDefault(THasNotNils)
118118

119-
reject default(TRefObjNotNil)
120-
reject userDefinedDefault(TRefObjNotNil)
121-
reject genericDefault(TRefObjNotNil)
119+
# produces only warning: reject default(TRefObjNotNil)
120+
# produces only warning: reject userDefinedDefault(TRefObjNotNil)
121+
# produces only warning: reject genericDefault(TRefObjNotNil)
122122

123123
# missing not nils in base
124124
reject TBaseHasNotNils()
125-
reject default(TBaseHasNotNils)
126-
reject userDefinedDefault(TBaseHasNotNils)
127-
reject genericDefault(TBaseHasNotNils)
125+
# produces only warning: reject default(TBaseHasNotNils)
126+
# produces only warning: reject userDefinedDefault(TBaseHasNotNils)
127+
# produces only warning: reject genericDefault(TBaseHasNotNils)
128128

129129
# once you take care of them, it's ok
130130
accept TBaseHasNotNils(a: notNilRef, b: notNilRef, choice: D)
@@ -163,8 +163,8 @@ accept((ref PartialRequiresInit)(a: 20))
163163
reject((ref PartialRequiresInit)(b: "x"))
164164
reject((ref PartialRequiresInit)())
165165

166-
reject default(PartialRequiresInit)
167-
reject userDefinedDefault(PartialRequiresInit)
166+
# produces only warning: reject default(PartialRequiresInit)
167+
# produces only warning: reject userDefinedDefault(PartialRequiresInit)
168168
reject:
169169
var obj: PartialRequiresInit
170170

@@ -181,8 +181,8 @@ reject((ref FullRequiresInit)(a: 10))
181181
reject((ref FullRequiresInit)(b: 20))
182182
reject((ref FullRequiresInit)())
183183

184-
reject default(FullRequiresInit)
185-
reject userDefinedDefault(FullRequiresInit)
184+
# produces only warning: reject default(FullRequiresInit)
185+
# produces only warning: reject userDefinedDefault(FullRequiresInit)
186186
reject:
187187
var obj: FullRequiresInit
188188

@@ -192,8 +192,8 @@ reject FullRequiresInitWithParent(a: notNilRef, b: nil, c: nil, e: 10, d: 20) #
192192
reject FullRequiresInitWithParent(a: notNilRef, b: notNilRef, e: 10, d: 20) # c should not be missing
193193
reject FullRequiresInitWithParent(a: notNilRef, b: notNilRef, c: nil, e: 10) # d should not be missing
194194
reject FullRequiresInitWithParent()
195-
reject default(FullRequiresInitWithParent)
196-
reject userDefinedDefault(FullRequiresInitWithParent)
195+
# produces only warning: reject default(FullRequiresInitWithParent)
196+
# produces only warning: reject userDefinedDefault(FullRequiresInitWithParent)
197197
reject:
198198
var obj: FullRequiresInitWithParent
199199

@@ -203,28 +203,36 @@ accept default(TNestedChoices)
203203
accept:
204204
var obj: TNestedChoices
205205

206+
#[# produces only warning:
206207
reject:
207208
# This proc is illegal, because it tries to produce
208209
# a default object of a type that requires initialization:
209210
proc defaultHasNotNils: THasNotNils =
210211
discard
212+
#]#
211213

214+
#[# produces only warning:
212215
reject:
213216
# You cannot cheat by using the result variable to specify
214217
# only some of the fields
215218
proc invalidPartialTHasNotNils: THasNotNils =
216219
result.c = nilRef
220+
#]#
217221

222+
#[# produces only warning:
218223
reject:
219224
# The same applies for requiresInit types
220225
proc invalidPartialRequiersInit: PartialRequiresInit =
221226
result.b = "x"
227+
#]#
222228

229+
#[# produces only warning:
223230
# All code paths must return a value when the result requires initialization:
224231
reject:
225232
proc ifWithoutAnElse: THasNotNils =
226233
if stdin.readLine == "":
227234
return THasNotNils(a: notNilRef, b: notNilRef, c: nilRef)
235+
#]#
228236

229237
accept:
230238
# All code paths must return a value when the result requires initialization:
@@ -234,6 +242,7 @@ accept:
234242
else:
235243
return THasNotNIls(a: notNilRef, b: notNilRef)
236244

245+
#[# produces only warning:
237246
reject:
238247
proc caseWithoutAllCasesCovered: FullRequiresInit =
239248
# Please note that these is no else branch here:
@@ -242,6 +251,7 @@ reject:
242251
return FullRequiresInit(a: 10, b: 20)
243252
of "y":
244253
return FullRequiresInit(a: 30, b: 40)
254+
#]#
245255

246256
accept:
247257
proc wellFormedCase: FullRequiresInit =
@@ -276,18 +286,24 @@ block:
276286
var one = legalSeq[0]
277287
var twoAgain = legalSeq.pop
278288

289+
#[# produces only warning:
279290
# It's not possible to tell the sequence to create elements
280291
# for us though:
281292
reject:
282293
var illegalSeq = newSeq[IllegalToConstruct](10)
294+
#]#
283295

296+
#[# produces only warning:
284297
reject:
285298
var illegalSeq: seq[IllegalToConstruct]
286299
newSeq(illegalSeq, 10)
300+
#]#
287301

302+
#[# produces only warning:
288303
reject:
289304
var illegalSeq: seq[IllegalToConstruct]
290305
illegalSeq.setLen 10
306+
#]#
291307

292308
# You can still use newSeqOfCap to write efficient code:
293309
var anotherLegalSequence = newSeqOfCap[IllegalToConstruct](10)
@@ -363,8 +379,10 @@ block:
363379
reject:
364380
var x: IllegalPair
365381

382+
#[# produces only warning:
366383
reject:
367384
var s = newSeq[IllegalPair](10)
385+
#]#
368386

369387
# Specific issues:
370388
#

tests/objects/tobjects_issues.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ block t3038:
113113
Type = ref object of RootObj
114114
SubType[T] = ref object of Type
115115
data: Data[T]
116-
SubSubType = ref object of SubType
116+
SubSubType = ref object of SubType[int]
117117
SubSubSubType = ref object of SubSubType

0 commit comments

Comments
 (0)