Skip to content

Commit

Permalink
still debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
saem committed Aug 27, 2022
1 parent 366ea79 commit 71b7125
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 86 deletions.
2 changes: 1 addition & 1 deletion compiler/ast/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export ast_types, ast_idgen, ast_query, int128
var ggDebug* {.deprecated.}: bool ## convenience switch for trying out things

when defined(useNodeIds):
const nodeIdToDebug* = 1944924 # 2322968
const nodeIdToDebug* = 1945378 # 2322968

proc addAllowNil*(father, son: Indexable) {.inline.} =
father.sons.add(son)
Expand Down
6 changes: 6 additions & 0 deletions compiler/ast/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,12 @@ proc typeMismatch*(
result = newError(conf, n, rsemTypeMismatch, conf.store(info, rep), instLoc())
result.info = info

if true or result.reportId.int == 24:
echo "error source stack trace start"
writeStackTrace()
echo "error source stack trace end"
echo "n id: ", n.id, " report id: ", result.reportId

# conf.localReport(result)

proc semReportTypeMismatch*(
Expand Down
6 changes: 6 additions & 0 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,12 @@ proc reportBody*(conf: ConfigRef, r: SemReport): string =

result.add " but expected '$1'" % x

result.add " node id: $1" % $r.ast.id
result.add " from: $1" % $r.reportInst

if r.ast.id == 1956360:
debug r.ast

if verbose:
result.addDeclaredLoc(conf, formal)

Expand Down
2 changes: 2 additions & 0 deletions compiler/sem/evaltempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
result.add res
else:
result.add newNodeI(nkEmpty, templ.info)
of nkError:
c.config.internalError(templ.info, "aw cwap")
else:
let parentIsDeclarative = c.isDeclarative
if templ.kind in routineDefs + {nkTypeSection, nkVarSection, nkLetSection, nkConstSection}:
Expand Down
11 changes: 8 additions & 3 deletions compiler/sem/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =

# XXX: why don't we set the `typ` field to formal like above and below?
result = typeMismatch(c.config, info, formal, arg.typ, arg)

else:
result = indexTypesMatch(c, formal, arg.typ, arg)
if result == nil:
Expand All @@ -195,6 +194,8 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
result = fitNodePostMatch(c, formal, result)

proc fitNodeConsiderViewType(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
if arg.isError:
return arg
let a = fitNode(c, formal, arg, info)
if formal.kind in {tyVar, tyLent}:
#classifyViewType(formal) != noView:
Expand Down Expand Up @@ -612,8 +613,12 @@ proc semMacroExpr(c: PContext, n: PNode, sym: PSym,
popInfoContext(c.config)

proc forceBool(c: PContext, n: PNode): PNode =
result = fitNode(c, getSysType(c.graph, n.info, tyBool), n, n.info)
if result == nil: result = n
case n.kind
of nkError:
result = n
else:
result = fitNode(c, getSysType(c.graph, n.info, tyBool), n, n.info)
if result == nil: result = n

proc semConstBoolExpr(c: PContext, n: PNode): PNode =
result = forceBool(c, semConstExpr(c, n))
Expand Down
2 changes: 2 additions & 0 deletions compiler/sem/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ proc indexTypesMatch(c: PContext, f, a: PType, arg: PNode): PNode =

proc inferWithMetatype(c: PContext, formal: PType,
arg: PNode, coerceDistincts = false): PNode =
if arg.isError:
return arg
var m = newCandidate(c, formal)
m.coerceDistincts = coerceDistincts
result = paramTypesMatch(m, formal, arg.typ, arg)
Expand Down
94 changes: 65 additions & 29 deletions compiler/sem/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,13 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode =
let
lhs = n[0]
rhs = semExprWithType(c, n[1], {})

if lhs.isError or rhs.isError:
n[0] = lhs
n[1] = rhs
result = c.config.wrapError(n)
return

if lhs.kind == nkSym and lhs.sym.kind == skResult:
n.typ = c.enforceVoidContext
if c.p.owner.kind != skMacro and resultTypeIsInferrable(lhs.sym.typ):
Expand Down Expand Up @@ -2487,14 +2494,22 @@ proc tryExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
var err: string
try:
result = semExpr(c, n, flags)
if result != nil and efNoSem2Check notin flags:
trackStmt(c, c.module, result, isTopLevel = false)
if c.config.errorCounter != oldErrorCount and
result != nil and result.kind != nkError:
result = nil

if result.isNil or result.isError:
# xxx: we should eliminate nils being produced in the first place
discard # just propogate the result
else:
# we got some non-error result, check for effects and error count
if efNoSem2Check notin flags:
trackStmt(c, c.module, result, isTopLevel = false)

if c.config.errorCounter != oldErrorCount:
# xxx: this nil should be an nkError
result = nil
except ERecoverableError:
discard
# undo symbol table changes (as far as it's possible):
# xxx: you'd think "as far as it's possible" above would have been a big clue
c.compilesContextId = oldCompilesId
c.generics = oldGenerics
c.inGenericContext = oldInGenericContext
Expand All @@ -2514,23 +2529,20 @@ proc tryExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =

proc semCompiles(c: PContext, n: PNode, flags: TExprFlags): PNode =
# we replace this node by a 'true' or 'false' node:
if n.len != 2: return semDirectOp(c, n, flags)
if n.len != 2:
return semDirectOp(c, n, flags)

# xxx: need to further confirm, but `n[1]` below might need to be copied
# defensively, as inclusion of nkError nodes may mutate the original AST
# that was passed in via the compiles call.

let
exprVal = tryExpr(c, n[1], flags)
exprVal = tryExpr(c, n[1].copyTree, flags)
didCompile = exprVal != nil and exprVal.kind != nkError
## this is the one place where we don't propagate nkError, wrapping the
## parent because this is a `compiles` call and should not leak across
## the AST boundary

if exprVal.isError and exprVal.id == 1944943:
# debug exprVal
echo "didCompile: ", didCompile

result = newIntNode(nkIntLit, ord(didCompile))
result.info = n.info
result.typ = getSysType(c.graph, n.info, tyBool)
Expand Down Expand Up @@ -2573,6 +2585,8 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
result.typ = makePtrType(c, result[1].typ)
of mTypeOf:
markUsed(c, n.info, s)
# xxx: defensively copy `n` because sem has a habit of mutating things
# result = semTypeOf(c, n.copyTree)
result = semTypeOf(c, n)
of mDefined:
markUsed(c, n.info, s)
Expand All @@ -2585,6 +2599,8 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
result = semDeclared(c, setMs(n, s), true)
of mCompiles:
markUsed(c, n.info, s)
# xxx: defensively copy `n` because sem has a habit of mutating things
# result = semCompiles(c, setMs(n.copyTree, s), flags)
result = semCompiles(c, setMs(n, s), flags)
of mIs:
markUsed(c, n.info, s)
Expand Down Expand Up @@ -2663,17 +2679,26 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
# when nimvm:
# ...
# else:
# ...
var whenNimvm = false
# ...
let whenNimvm =
if n.len == 2 and n[0].kind == nkElifBranch and
n[1].kind == nkElse:
let exprNode = n[0][0]

case exprNode.kind
of nkIdent:
lookUp(c, exprNode).magic == mNimvm
of nkSym:
exprNode.sym.magic == mNimvm
else:
false
else:
false

if whenNimvm:
n.flags.incl nfLL

var typ = commonTypeBegin
if n.len == 2 and n[0].kind == nkElifBranch and
n[1].kind == nkElse:
let exprNode = n[0][0]
if exprNode.kind == nkIdent:
whenNimvm = lookUp(c, exprNode).magic == mNimvm
elif exprNode.kind == nkSym:
whenNimvm = exprNode.sym.magic == mNimvm
if whenNimvm: n.flags.incl nfLL

for i in 0..<n.len:
var it = n[i]
Expand All @@ -2687,13 +2712,24 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
result = n # when nimvm is not elimited until codegen
else:
let e = forceBool(c, semConstExpr(c, it[0]))
if e.kind != nkIntLit:
# can happen for cascading errors, assume false
# InternalError(n.info, "semWhen")
discard
elif e.intVal != 0 and result == nil:
setResult(it[1])
return # we're not in nimvm and we already have a result

case e.kind
of nkError:
discard # can happen for cascading errors, assume false
of nkIntLit:
if e.intVal != 0 and result == nil:
setResult(it[1])
return # we're not in nimvm and we already have a result
else:
discard # xxx: should probably internal error

# if e.kind != nkIntLit:
# # can happen for cascading errors, assume false
# # InternalError(n.info, "semWhen")
# discard
# elif e.intVal != 0 and result == nil:
# setResult(it[1])
# return # we're not in nimvm and we already have a result
of nkElse, nkElseExpr:
checkSonsLen(it, 1, c.config)
if result == nil or whenNimvm:
Expand Down Expand Up @@ -3434,7 +3470,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
else:
result = semIndirectOp(c, n, flags)

if nfDefaultRefsParam in result.flags:
if result.kind != nkError and nfDefaultRefsParam in result.flags:
result = result.copyTree #XXX: Figure out what causes default param nodes to be shared.. (sigmatch bug?)
# We've found a default value that references another param.
# See the notes in `hoistParamsUsedInDefault` for more details.
Expand Down
8 changes: 4 additions & 4 deletions compiler/sem/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,10 @@ proc semPrivateAccess(c: PContext, n: PNode): PNode =
proc magicsAfterOverloadResolution(c: PContext, n: PNode,
flags: TExprFlags): PNode =
## This is the preferred code point to implement magics.
## ``c`` the current module, a symbol table to a very good approximation
## ``n`` the ast like it would be passed to a real macro
## ``flags`` Some flags for more contextual information on how the
## "macro" is calld.
## `c` the current module, a symbol table to a very good approximation
## `n` the ast like it would be passed to a real macro
## `flags` Some flags for more contextual information on how the
## "macro" is called.

case n[0].sym.magic
of mAddr:
Expand Down
59 changes: 30 additions & 29 deletions compiler/sem/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,13 @@ proc semNormalizedLetOrVar(c: PContext, n: PNode, symkind: TSymKind): PNode =
case temp.kind
of nkSymChoices:
if temp[0].typ.skipTypes(abstractInst).kind == tyEnum:
hasError = true
newError(c.config, temp, newSymChoiceUseQualifierReport(temp))
else:
temp
of nkError:
hasError = true
temp
else:
temp
initType =
Expand Down Expand Up @@ -2673,27 +2677,14 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,

# xxx: use a defer for pushOwner/openScope automatic closing

result[patternPos] =
case n[patternPos].kind
of nkEmpty:
c.graph.emptyNode
of nkError:
n[patternPos]
else:
# xxx: `semPattern` mutates `n[patternPos]`, this needs fixing
semPattern(c, n[patternPos], s)

if result[patternPos].kind == nkError:
# c.config.localReport(n[patternPos])
hasError = true

# if result.len > namePos:
# result[namePos] = n[namePos]
# if result.len > genericParamsPos:
# result[genericParamsPos] = n[genericParamsPos]
if result[namePos].isError:
hasError = true

if result.len > patternPos:
if n[patternPos].isError:
hasError = true
result[patternPos] = n[patternPos]

if result.len > paramsPos:
if n[paramsPos].isError:
hasError = true
Expand All @@ -2708,7 +2699,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
if n[miscPos].isError:
hasError = true
result[miscPos] = n[miscPos]

if result.len > bodyPos:
if n[bodyPos].isError:
hasError = true
Expand All @@ -2718,6 +2709,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
if n[resultPos].isError:
hasError = true
result[resultPos] = n[resultPos]
result[resultPos].sym.typ = s.typ[0]

if result.len > dispatcherPos:
if n[dispatcherPos].isError:
Expand Down Expand Up @@ -2782,14 +2774,30 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
result[genericParamsPos] = result[miscPos][1]
result[miscPos] = c.graph.emptyNode

if n.len > resultPos:
# xxx: gets updated later in `addResult` or `maybeAddResult`
result[resultPos] = n[resultPos].copyTree
if result.len > resultPos:
# xxx: gets updated later in `addResult` or `maybeAddResult`, hack to make
# `result` symbol swapping work when successively analysing the same
# proc-like. all this because the legacy analysis approach mutated `n`
# in-place.
result[resultPos].sym.typ = s.typ[0]

if tfTriggersCompileTime in s.typ.flags:
incl(s.flags, sfCompileTime)

result[patternPos] =
case n[patternPos].kind
of nkEmpty:
c.graph.emptyNode
of nkError:
n[patternPos]
else:
# xxx: `semPattern` mutates `n[patternPos]`, this needs fixing
semPattern(c, n[patternPos], s)

if result[patternPos].kind == nkError:
# c.config.localReport(n[patternPos])
hasError = true

if s.kind == skIterator:
s.typ.flags.incl(tfIterator)
elif s.kind == skFunc:
Expand Down Expand Up @@ -3043,9 +3051,6 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,

if hasError:
result = c.config.wrapError(result)
if s.ast[bodyPos].isError:
# debug result
writeStackTrace()
return

if n[patternPos].kind != nkEmpty:
Expand Down Expand Up @@ -3522,10 +3527,6 @@ proc semStmtList(c: PContext, n: PNode, flags: TExprFlags): PNode =

if hasError and result.kind != nkError:
result = wrapError(c.config, result)

# if result.kind == nkError:
# if result.id == 1944464 or result[wrongNodePos].id == 1944464:
# echo result.id

proc semStmt(c: PContext, n: PNode; flags: TExprFlags): PNode =
if efInTypeof in flags:
Expand Down
Loading

0 comments on commit 71b7125

Please sign in to comment.