mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-13 06:43:52 +00:00
fixes #22926
(cherry picked from commit 96513b2506)
This commit is contained in:
@@ -237,6 +237,12 @@ proc fitDefaultNode(c: PContext, n: PNode): PType =
|
||||
if n[^2].kind != nkEmpty:
|
||||
if expectedType != nil and oldType != expectedType:
|
||||
n[^1] = fitNodeConsiderViewType(c, expectedType, n[^1], n[^1].info)
|
||||
changeType(c, n[^1], expectedType, true) # infer types for default fields value
|
||||
# bug #22926; be cautious that it uses `semConstExpr` to
|
||||
# evaulate the default fields; it's only natural to use
|
||||
# `changeType` to infer types for constant values
|
||||
# that's also the reason why we don't use `semExpr` to check
|
||||
# the type since two overlapping error messages might be produced
|
||||
result = n[^1].typ
|
||||
else:
|
||||
result = n[^1].typ
|
||||
|
||||
@@ -4,16 +4,13 @@ discard """
|
||||
nimout:
|
||||
'''
|
||||
tdefaultfieldscheck.nim(14, 17) Error: type mismatch: got <string> but expected 'int'
|
||||
tdefaultfieldscheck.nim(15, 20) Error: type mismatch: got <int literal(12)> but expected 'string'
|
||||
tdefaultfieldscheck.nim(17, 16) Error: type mismatch: got <float64> but expected 'int'
|
||||
'''
|
||||
"""
|
||||
|
||||
|
||||
type
|
||||
Date* = object
|
||||
name: int = "string"
|
||||
time: string = 12
|
||||
goal: float = 7
|
||||
fun: int = 1.4
|
||||
name: int = "string"
|
||||
|
||||
echo default(Date)
|
||||
echo default(Date)
|
||||
|
||||
@@ -717,6 +717,33 @@ template main {.dirty.} =
|
||||
|
||||
doAssert T().kind == B
|
||||
|
||||
block: # bug #22926
|
||||
type
|
||||
Direction = enum
|
||||
North
|
||||
South
|
||||
East
|
||||
West
|
||||
|
||||
ArrayObj1 = object
|
||||
list: array[Direction, int]
|
||||
|
||||
ArrayObj2 = object
|
||||
list: array[Direction, int] = [1, 2, 3, 4]
|
||||
|
||||
block:
|
||||
var a: ArrayObj1
|
||||
doAssert a.list[West] == 0
|
||||
var b = default ArrayObj1
|
||||
doAssert b.list[North] == 0
|
||||
|
||||
|
||||
block:
|
||||
var a: ArrayObj2
|
||||
doAssert a.list[West] == 0
|
||||
var b = default ArrayObj2
|
||||
doAssert b.list[North] == 1
|
||||
|
||||
|
||||
static: main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user