Skip to content
Open
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
35 changes: 31 additions & 4 deletions compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,27 @@ proc subtypeRelation(g: ModuleGraph; spec, real: PNode): bool =
else:
return safeInheritanceDiff(g.excType(real), spec.typ) <= 0

proc traceForbidsOrigin(g: ModuleGraph; r: PNode; conf: ConfigRef) =
var current = r
var visited = initIntSet()
while current.kind in nkCallKinds and current[0].kind == nkSym:
let s = current[0].sym
if s.id in visited: break
visited.incl(s.id)
if s.typ == nil or s.typ.n == nil or s.typ.n[0].kind != nkEffectList: break
let effectList = s.typ.n[0]
if effectList.len == 0: break
let tags = effectList[tagEffects]
if tags == nil: break
var next: PNode = nil
for tag in items(tags):
if tag.typ != nil and sameType(tag.typ.skipTypes(skipPtrs), r.typ.skipTypes(skipPtrs)):
next = tag
break
if next == nil or next.info == current.info: break
message(conf, next.info, hintUser, "effect propagated from here")
current = next

proc checkRaisesSpec(g: ModuleGraph; emitWarnings: bool; spec, real: PNode, msg: string, hints: bool;
effectPredicate: proc (g: ModuleGraph; a, b: PNode): bool {.nimcall.};
hintsArg: PNode = nil; isForbids: bool = false; unknownRaises: seq[(PSym, TLineInfo)] = @[]) =
Expand All @@ -1731,14 +1752,20 @@ proc checkRaisesSpec(g: ModuleGraph; emitWarnings: bool; spec, real: PNode, msg:
if isForbids:
break search
# XXX call graph analysis would be nice here!
pushInfoContext(g.config, spec.info)
var rr = if r.kind == nkRaiseStmt: r[0] else: r
while rr.kind in {nkStmtList, nkStmtListExpr} and rr.len > 0: rr = rr.lastSon
for (s, info) in unknownRaises.items:
message(g.config, info, hintUnknownRaises, s.name.s)
message(g.config, r.info, if emitWarnings: warnEffect else: errGenerated,
renderTree(rr) & " " & msg & typeToString(r.typ))
popInfoContext(g.config)
if isForbids:
message(g.config, spec.info, hintUser, ".forbids spec declared here")
traceForbidsOrigin(g, r, g.config)
message(g.config, r.info, if emitWarnings: warnEffect else: errGenerated,
renderTree(rr) & " " & msg & typeToString(r.typ))
else:
pushInfoContext(g.config, spec.info)
message(g.config, r.info, if emitWarnings: warnEffect else: errGenerated,
renderTree(rr) & " " & msg & typeToString(r.typ))
popInfoContext(g.config)
# hint about unnecessarily listed exception types:
if hints:
for s in 0..<spec.len:
Expand Down
18 changes: 18 additions & 0 deletions tests/effects/tforbids_trace.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
discard """
action: compile
errormsg: "c() has an illegal effect: NestedPoll"
line: 18
"""

type
NestedPoll* = object of RootEffect

proc poll*() {.tags: [NestedPoll].} =
discard

proc a*() = poll()
proc b*() = a()
proc c*() = b()

proc test*() {.forbids: [NestedPoll].} =
c()
Loading