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

Extended side effect error messages #18418

Merged
merged 11 commits into from
Jul 15, 2021
11 changes: 6 additions & 5 deletions compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ proc listGcUnsafety(s: PSym; onlyWarning: bool; conf: ConfigRef) =
var cycleCheck = initIntSet()
listGcUnsafety(s, onlyWarning, cycleCheck, conf)

proc listSideEffects(result: var string; s: PSym; cycleCheck: var IntSet; conf: ConfigRef; context: PContext; indentLevel: int) =
proc listSideEffects(result: var string; initialNoSideEffectSym, s: PSym;
cycleCheck: var IntSet; conf: ConfigRef; context: PContext; indentLevel: int) =
template addHint(msg; lineInfo; sym; level = indentLevel) =
result.addf "$# $# Hint: '$#' $#\n" % [repeat(">", level), conf $ lineInfo, sym, msg]
if context.sideEffects.hasKey(s.id):
Expand All @@ -276,11 +277,11 @@ proc listSideEffects(result: var string; s: PSym; cycleCheck: var IntSet; conf:
case u.kind
of skLet, skVar:
addHint("accesses global state '$#'" % u.name.s, useLineInfo, s.name.s)
addHint("accessed by `.noSideEffect` '$#'" % s.name.s, u.info, u.name.s, indentLevel + 1)
addHint("accessed by `.noSideEffect` '$#'" % initialNoSideEffectSym.name.s, u.info, u.name.s, indentLevel + 1)
of routineKinds:
addHint("calls `.sideEffect` '$#'" % u.name.s, useLineInfo, s.name.s)
addHint("called by `.noSideEffect` '$#'" % s.name.s, u.info, u.name.s, indentLevel + 1)
listSideEffects(result, u, cycleCheck, conf, context, indentLevel + 2)
addHint("called by `.noSideEffect` '$#'" % initialNoSideEffectSym.name.s, u.info, u.name.s, indentLevel + 1)
listSideEffects(result, initialNoSideEffectSym, u, cycleCheck, conf, context, indentLevel + 2)
of skParam, skForVar:
addHint("calls routine via hidden pointer indirection", useLineInfo, s.name.s)
else:
Expand All @@ -289,7 +290,7 @@ proc listSideEffects(result: var string; s: PSym; cycleCheck: var IntSet; conf:
proc listSideEffects(result: var string; s: PSym; conf: ConfigRef; context: PContext) =
var cycleCheck = initIntSet()
result.addf "'$#' can have side effects\n" % s.name.s
listSideEffects(result, s, cycleCheck, conf, context, 1)
listSideEffects(result, s, s, cycleCheck, conf, context, 1)

proc useVarNoInitCheck(a: PEffects; n: PNode; s: PSym) =
if {sfGlobal, sfThread} * s.flags != {} and s.kind in {skVar, skLet} and
Expand Down
27 changes: 14 additions & 13 deletions tests/effects/tdiagnostic_messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ discard """
nimoutFull: true
action: "reject"
cmd: "nim r --hint:Conf:off $file"
nimout: '''tdiagnostic_messages.nim(35, 6) Error: 'a' can have side effects
> tdiagnostic_messages.nim(36, 30) Hint: 'a' calls `.sideEffect` 'callWithSideEffects'
>> tdiagnostic_messages.nim(28, 6) Hint: 'callWithSideEffects' called by `.noSideEffect` 'a'
>>> tdiagnostic_messages.nim(30, 34) Hint: 'callWithSideEffects' calls `.sideEffect` 'indirectCallViaVarParam'
>>>> tdiagnostic_messages.nim(24, 6) Hint: 'indirectCallViaVarParam' called by `.noSideEffect` 'callWithSideEffects'
>>>>> tdiagnostic_messages.nim(25, 7) Hint: 'indirectCallViaVarParam' calls routine via hidden pointer indirection
>>> tdiagnostic_messages.nim(31, 33) Hint: 'callWithSideEffects' calls `.sideEffect` 'indirectCallViaPointer'
>>>> tdiagnostic_messages.nim(26, 6) Hint: 'indirectCallViaPointer' called by `.noSideEffect` 'callWithSideEffects'
>>>>> tdiagnostic_messages.nim(27, 32) Hint: 'indirectCallViaPointer' calls routine via pointer indirection
>>> tdiagnostic_messages.nim(32, 10) Hint: 'callWithSideEffects' calls `.sideEffect` 'myEcho'
>>>> tdiagnostic_messages.nim(23, 6) Hint: 'myEcho' called by `.noSideEffect` 'callWithSideEffects'
>>> tdiagnostic_messages.nim(33, 3) Hint: 'callWithSideEffects' accesses global state 'globalVar'
>>>> tdiagnostic_messages.nim(22, 5) Hint: 'globalVar' accessed by `.noSideEffect` 'callWithSideEffects'
nimout: '''
tdiagnostic_messages.nim(36, 6) Error: 'a' can have side effects
> tdiagnostic_messages.nim(37, 30) Hint: 'a' calls `.sideEffect` 'callWithSideEffects'
>> tdiagnostic_messages.nim(29, 6) Hint: 'callWithSideEffects' called by `.noSideEffect` 'a'
>>> tdiagnostic_messages.nim(31, 34) Hint: 'callWithSideEffects' calls `.sideEffect` 'indirectCallViaVarParam'
>>>> tdiagnostic_messages.nim(25, 6) Hint: 'indirectCallViaVarParam' called by `.noSideEffect` 'a'
>>>>> tdiagnostic_messages.nim(26, 7) Hint: 'indirectCallViaVarParam' calls routine via hidden pointer indirection
>>> tdiagnostic_messages.nim(32, 33) Hint: 'callWithSideEffects' calls `.sideEffect` 'indirectCallViaPointer'
>>>> tdiagnostic_messages.nim(27, 6) Hint: 'indirectCallViaPointer' called by `.noSideEffect` 'a'
>>>>> tdiagnostic_messages.nim(28, 32) Hint: 'indirectCallViaPointer' calls routine via pointer indirection
>>> tdiagnostic_messages.nim(33, 10) Hint: 'callWithSideEffects' calls `.sideEffect` 'myEcho'
>>>> tdiagnostic_messages.nim(24, 6) Hint: 'myEcho' called by `.noSideEffect` 'a'
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
>>> tdiagnostic_messages.nim(34, 3) Hint: 'callWithSideEffects' accesses global state 'globalVar'
>>>> tdiagnostic_messages.nim(23, 5) Hint: 'globalVar' accessed by `.noSideEffect` 'a'

'''
"""
Expand Down