Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partially reworked semProcAux #422

Closed
wants to merge 6 commits into from
Closed
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
80 changes: 76 additions & 4 deletions compiler/ast/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,48 @@ export ast_types, ast_idgen, ast_query, int128, ast_parsed_types
var ggDebug* {.deprecated.}: bool ## convenience switch for trying out things

when defined(useNodeIds):
const nodeIdToDebug* = -1 # 2322968

proc addAllowNil*(father, son: Indexable) {.inline.} =
father.sons.add(son)
# error node lineage
# const nodeIdToDebug* = 1973029 # 2322968
# const nodeIdToDebug* = 1974076 # 2322968
# const nodeIdToDebug* = 1970808 # 2322968
# const nodeIdToDebug* = 1969943 # 2322968
# const nodeIdToDebug* = 1969113 # 2322968
# const nodeIdToDebug* = 1969074 # 2322968
# const nodeIdToDebug* = 1968236 # 2322968
# const nodeIdToDebug* = 1643461 # 2322968

# sym node lineage
# const nodeIdToDebug* = 1974077 # 2322968
# const nodeIdToDebug* = 1970808 # 2322968
# const nodeIdToDebug* = 1969943 # 2322968
# const nodeIdToDebug* = 1969114 # 2322968

# sym node lineage old
# const nodeIdToDebug* = 1974077 # 2322968
# const nodeIdToDebug* = 1970809 # 2322968
# const nodeIdToDebug* = 1969944 # 2322968
# const nodeIdToDebug* = 1969114 # 2322968

# sym error node lineage
# const nodeIdToDebug* = 1973029 # 2322968
# const nodeIdToDebug* = 1972974 # 2322968
# const nodeIdToDebug* = 1972933 # 2322968

# const nodeIdToDebug* = 1977361 # 2322968

# nested error node
# const nodeIdToDebug* = 1977350 # 2322968

# field access issues?
# const nodeIdToDebug* = 451496 # 2322968
# const nodeIdToDebug* = 451493 # 2322968
# const nodeIdToDebug* = 451490 # 2322968

# const nodeIdToDebug* = 451471 # 2322968 # pragma block
# const nodeIdToDebug* = 450986 # 2322968 # outer not found
# const nodeIdToDebug* = 451483 # 2322968 # inner not found

const nodeIdToDebug* = 1977350 # 2322968 # inner not found

var gNodeId: int
template setNodeId() =
Expand All @@ -51,6 +89,32 @@ template setNodeId() =
echo "KIND ", result.kind
writeStackTrace()

const debugOldNodeModification = false
var lastAnalysedNodeId = 0
## track largest node id we've seen at key points in the compiler to guard
## against modifying the past

when debugOldNodeModification:
proc markLastAnalysedNode*() {.inline.} =
{.cast(noSideEffect)}:
lastAnalysedNodeId = gNodeId

proc checkpointLastAnalysedNode*(): int {.inline.} =
{.cast(noSideEffect)}:
lastAnalysedNodeId = gNodeId
lastAnalysedNodeId

proc modifyingAnalysedNode*(n: PNode): bool {.inline.} =
{.cast(noSideEffect)}:
return n.id <= lastAnalysedNodeId
else:
template markLastAnalysedNode*() = discard
template checkpointLastAnalysedNode*(): int = discard
template modifyingAnalysedNode*(n: PNode): bool = discard

proc addAllowNil*(father, son: Indexable) {.inline.} =
father.sons.add(son)

proc newNodeAux(
kind: TNodeKind, info: TLineInfo, typ: PType, children: int
): PNode {.inline.} =
Expand Down Expand Up @@ -534,6 +598,14 @@ proc transitionToLet*(s: PSym) =
s.bitsize = obj.bitsize
s.alignment = obj.alignment

proc transitionToError*(s: PSym, ast: PNode) =
## convert `PSym` into an `skError` with error `ast`
assert s != nil
assert not s.ast.isError
transitionSymKindCommon(skError)
s.ast = ast
s.typ = ast.typ

proc shallowCopy*(src: PNode): PNode =
# does not copy its sons, but provides space for them:
copyNodeImpl(result, src):
Expand Down
17 changes: 9 additions & 8 deletions compiler/ast/errorreporting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ proc errorHandling*(err: PNode): TErrorHandling =

template localReport*(conf: ConfigRef, node: PNode) =
## Write out existing sem report that is stored in the nkError node
assert node.kind == nkError, $node.kind
{.line.}:
assert node.kind == nkError, $node.kind

when defined(nimDebugUnreportedErrors):
conf.unreportedErrors.del node.reportId
for err in walkErrors(conf, node):
conf.unreportedErrors.del err.reportId
when defined(nimDebugUnreportedErrors):
conf.unreportedErrors.del node.reportId
for err in walkErrors(conf, node):
conf.unreportedErrors.del err.reportId

for err in walkErrors(conf, node):
if true or canReport(conf, err):
handleReport(conf, err.reportId, instLoc(), node.errorHandling)
for err in walkErrors(conf, node):
if true or canReport(conf, err):
handleReport(conf, err.reportId, instLoc(), node.errorHandling)
6 changes: 6 additions & 0 deletions compiler/ast/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,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
7 changes: 7 additions & 0 deletions compiler/backend/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,13 @@ proc genProcNoForward(m: BModule, prc: PSym) =
symInDynamicLibPartial(m, prc)
elif sfImportc notin prc.flags:
var q = findPendingModule(m, prc)

if prc.ast.isError:
let n = prc.ast
echo "error node id: ", n.id, " info: ", m.g.graph.config $ n.info,
"\n inst: ", m.g.graph.config.getReport(n.reportId).reportInst,
"\n from: ", m.g.graph.config.getReport(n.reportId).reportFrom

fillProcLoc(q, prc.ast[namePos])
# generate a getProc call to initialize the pointer for this
# externally-to-the-current-module defined proc, also important
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 @@ -1070,6 +1070,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
8 changes: 7 additions & 1 deletion compiler/sem/evaltempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import
msgs
],
compiler/utils/[
debugutils
debugutils,
astrepr
]

type
Expand All @@ -50,6 +51,8 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
case x.kind
of nkArgList:
for y in items(x): result.add(y)
of nkError:
c.config.localReport(param)
else:
result.add copyTree(x)

Expand Down Expand Up @@ -189,6 +192,9 @@ proc evalTemplate*(n: PNode, tmpl, genSymOwner: PSym;

result = n

if n.kind == nkError:
return

# replace each param by the corresponding node:
let args = evalTemplateArgs(n, tmpl, conf, fromHlo)
var ctx = TemplCtx(
Expand Down
3 changes: 2 additions & 1 deletion compiler/sem/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ proc genOp(c: var Con; t: PType; kind: TTypeAttachedOp; dest, ri: PNode): PNode
dbg:
if kind == attachedDestructor:
echo "destructor is ", op.id, " ", op.ast
if sfError in op.flags: checkForErrorPragma(c, t, ri, AttachedOpToStr[kind])
if sfError in op.flags:
checkForErrorPragma(c, t, ri, AttachedOpToStr[kind])
c.genOp(op, dest)

proc genDestroy(c: var Con; dest: PNode): PNode =
Expand Down
5 changes: 5 additions & 0 deletions compiler/sem/lambdalifting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import
types,
lineinfos
],
compiler/utils/astrepr,
compiler/modules/[
magicsys,
modulegraphs
Expand Down Expand Up @@ -528,6 +529,10 @@ proc detectCapturedVars(n: PNode; owner: PSym; c: var DetectionPass) =
detectCapturedVars(n[namePos], owner, c)
of nkReturnStmt:
detectCapturedVars(n[0], owner, c)
# of nkError:
# echo "error node id: ", n.id, " info: ", c.graph.config $ n.info,
# "\n inst: ", c.graph.config.getReport(n.reportId).reportInst,
# "\n from: ", c.graph.config.getReport(n.reportId).reportFrom
else:
for i in 0..<n.len:
detectCapturedVars(n[i], owner, c)
Expand Down
2 changes: 0 additions & 2 deletions compiler/sem/liftdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ proc getCycleParam(c: TLiftCtx): PNode =
result = boolLit(c.g, c.info, true)

proc newHookCall(c: var TLiftCtx; op: PSym; x, y: PNode): PNode =
#if sfError in op.flags:
# localReport(c.config, x.info, "usage of '$1' is a user-defined error" % op.name.s)
result = newNodeI(nkCall, x.info)
result.add newSymNode(op)
if sfNeverRaises notin op.flags:
Expand Down
1 change: 0 additions & 1 deletion compiler/sem/lookups.nim
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ proc newQualifiedLookUpError(c: PContext, ident: PIdent, info: TLineInfo, err: P
result = newSym(skError, ident, nextSymId(c.idgen), getCurrOwner(c), info)
result.typ = c.errorType
result.flags.incl(sfDiscardable)
# result.flags.incl(sfError)
result.ast = err

proc errorSym*(c: PContext, n, err: PNode): PSym =
Expand Down
27 changes: 23 additions & 4 deletions compiler/sem/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,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 @@ -198,10 +197,17 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
# XXX: is this "error correction" or actually "fitting" the node?
result = copyTree(arg)
result.typ = formal

if result.id == 1977350:
echo "fitNode arg id: ", arg.id,
" arg info: ", c.config $ arg.info,
" info: ", c.config $ info
else:
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 @@ -530,12 +536,21 @@ proc semAfterMacroCall(c: PContext, call, macroResult: PNode,
## coherence, making sure that variables declared with 'let' aren't
## reassigned, and binding the unbound identifiers that the macro output
## contains.
assert not s.isError

inc(c.config.evalTemplateCounter)

if c.config.evalTemplateCounter > evalTemplateLimit:
globalReport(c.config, s.info, SemReport(kind: rsemTemplateInstantiationTooNested))
c.friendModules.add(s.owner.getModule)

result = macroResult
resetSemFlag result

if result.isError:
return

c.friendModules.add(s.owner.getModule)

if s.typ[0] == nil:
result = semStmt(c, result, flags)
else:
Expand Down Expand Up @@ -623,8 +638,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
8 changes: 7 additions & 1 deletion compiler/sem/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1187,21 +1187,27 @@ proc markOwnerModuleAsUsed*(c: PContext; s: PSym) =
proc markUsed*(c: PContext; info: TLineInfo; s: PSym) =
let conf = c.config
incl(s.flags, sfUsed)

if s.kind == skEnumField and s.owner != nil:
incl(s.owner.flags, sfUsed)
if sfDeprecated in s.owner.flags:
warnAboutDeprecated(conf, info, s)

if {sfDeprecated, sfError} * s.flags != {}:
if sfDeprecated in s.flags:
if not (c.lastTLineInfo.line == info.line and
c.lastTLineInfo.col == info.col):
warnAboutDeprecated(conf, info, s)
c.lastTLineInfo = info

if sfError in s.flags: userError(conf, info, s)
if sfError in s.flags:
userError(conf, info, s)

when defined(nimsuggest):
if c.graph.onMarkUsed != nil:
c.graph.onMarkUsed(c.graph, info, s, c.graph.usageSym, false)

if {optStyleHint, optStyleError} * conf.globalOptions != {}:
styleCheckUse(conf, info, s)

markOwnerModuleAsUsed(c, s)
Loading