Skip to content

Commit 5d58557

Browse files
committed
Merge remote-tracking branch 'upstream/devel' into nimlsp5
2 parents bb34a45 + 5a7b975 commit 5d58557

33 files changed

+319
-298
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ jobs:
123123
run: echo "::add-matcher::.github/nim-problem-matcher.json"
124124

125125
- name: Build release binaries
126-
run: ./koch.py all
126+
run: ./koch.py all-strict
127127

128128
- name: Upload workspace to artifacts
129129
uses: ./.github/actions/upload-compiler

compiler/ast/ast_query.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,7 @@ proc isSinkType*(t: PType): bool {.inline.} =
617617
t.kind == tySink
618618

619619
const magicsThatCanRaise* = {
620-
mNone, mSlurp, mParseExprToAst, mParseStmtToAst, mEcho,
621-
mChckRange }
620+
mNone, mParseExprToAst, mParseStmtToAst, mEcho, mChckRange }
622621

623622
proc canRaiseConservative*(fn: PNode): bool =
624623
if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:

compiler/ast/ast_types.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ type
742742
mDefined, mDeclared, mDeclaredInScope, mCompiles, mArrGet, mArrPut, mAsgn,
743743
mLow, mHigh, mSizeOf, mAlignOf, mOffsetOf, mTypeTrait,
744744
mIs, mOf, mAddr, mType, mTypeOf,
745-
mPlugin, mEcho, mShallowCopy, mSlurp,
745+
mPlugin, mEcho, mShallowCopy,
746746
mStatic,
747747
mParseExprToAst, mParseStmtToAst, mExpandToAst, mQuoteAst,
748748
mInc, mDec, mOrd,

compiler/ast/renderer.nim

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,7 @@ proc atom(g: TSrcGen; n: PNode): string =
364364
if (n.typ != nil) and (n.typ.sym != nil): result = n.typ.sym.name.s
365365
else: result = "[type node]"
366366
else:
367-
g.config.localReport InternalReport(
368-
kind: rintUnreachable, msg: "renderer.atom " & $n.kind)
369-
result = ""
367+
g.config.internalError("renderer.atom " & $n.kind)
370368

371369
proc lcomma(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
372370
assert(theEnd < 0)
@@ -1638,8 +1636,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
16381636
gsub(g, n.sons[0], c)
16391637
else:
16401638
#nkNone, nkExplicitTypeListCall:
1641-
g.config.localReport InternalReport(
1642-
kind: rintUnreachable, msg: "renderer.gsub(" & $n.kind & ')')
1639+
g.config.internalError("renderer.gsub(" & $n.kind & ')')
16431640

16441641
proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string =
16451642
if n == nil: return "<nil tree>"

compiler/ast/report_enums.nim

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,10 @@ type
7171
repNone
7272

7373
#-------------------------- Internal reports ---------------------------#
74-
# Internal reports being
74+
# Internal reports begin
7575
# fatal errors begin
76-
rintUnknown ## Unknown internal report kind
77-
rintFatal ## Explicitly fatal compiler error
78-
7976
rintUnreachable ## State in the compiler code that must not be reached
8077
rintAssert ## Failed internal assert in the compiler
81-
82-
rintIce ## Internal compilation error
8378
# fatal end
8479

8580
# errors being
@@ -909,7 +904,7 @@ type
909904

910905
ExternalReportKind* = range[rextCmdRequiresFile .. rextPath]
911906

912-
InternalReportKind* = range[rintUnknown .. rintEchoMessage]
907+
InternalReportKind* = range[rintUnreachable .. rintEchoMessage]
913908

914909

915910
const
@@ -972,8 +967,8 @@ const
972967
repInternalKinds*: ReportKinds = {
973968
low(InternalReportKind) .. high(InternalReportKind)}
974969

975-
rintFatalKinds* = {rintUnknown .. rintIce} ## Fatal internal compilation
976-
## reports
970+
rintFatalKinds* = {rintUnreachable .. rintAssert} ## Fatal internal compilation
971+
## reports
977972
rintErrorKinds* = {rintCannotOpenFile .. rintNotImplemented}
978973
rintWarningKinds* = {rintWarnCannotOpenFile .. rintWarnFileChanged}
979974
rintHintKinds* = {rintSource .. rintSuccessX}

compiler/ast/types.nim

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ import
3535
modulegraphs,
3636
]
3737

38-
# TODO: switch to internalError/Assert or better yet emit the appropriate
39-
# diagnostic/event/telemetry data instead, then drop this dependency
40-
from compiler/ast/reports_internal import InternalReport
4138
from compiler/ast/report_enums import ReportKind
4239

4340
from compiler/ast/reports_sem import SemReport,
@@ -565,10 +562,7 @@ proc firstFloat*(t: PType): BiggestFloat =
565562
tyStatic, tyInferred, tyUserTypeClasses:
566563
firstFloat(lastSon(t))
567564
else:
568-
newPartialConfigRef().localReport InternalReport(
569-
kind: rintUnreachable,
570-
msg: "invalid kind for firstFloat(" & $t.kind & ')')
571-
NaN
565+
unreachable("invalid kind for firstFloat(" & $t.kind & ')')
572566

573567
proc lastFloat*(t: PType): BiggestFloat =
574568
case t.kind
@@ -582,10 +576,7 @@ proc lastFloat*(t: PType): BiggestFloat =
582576
tyStatic, tyInferred, tyUserTypeClasses:
583577
lastFloat(lastSon(t))
584578
else:
585-
newPartialConfigRef().localReport InternalReport(
586-
kind: rintUnreachable,
587-
msg: "invalid kind for firstFloat(" & $t.kind & ')')
588-
NaN
579+
unreachable("invalid kind for firstFloat(" & $t.kind & ')')
589580

590581
proc floatRangeCheck*(x: BiggestFloat, t: PType): bool =
591582
case t.kind
@@ -601,10 +592,7 @@ proc floatRangeCheck*(x: BiggestFloat, t: PType): bool =
601592
tyStatic, tyInferred, tyUserTypeClasses:
602593
floatRangeCheck(x, lastSon(t))
603594
else:
604-
newPartialConfigRef().localReport InternalReport(
605-
kind: rintUnreachable,
606-
msg: "invalid kind for floatRangeCheck(" & $t.kind & ')')
607-
false
595+
unreachable("invalid kind for floatRangeCheck(" & $t.kind & ')')
608596

609597
# -------------- type equality -----------------------------------------------
610598

compiler/backend/ccgexprs.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ proc genMagicExpr(p: BProc, e: CgNode, d: var TLoc, op: TMagic) =
18391839
of mDefault: genDefault(p, e, d)
18401840
of mEcho: genEcho(p, e[1].skipConv)
18411841
of mArrToSeq: genArrToSeq(p, e, d)
1842-
of mNLen..mNError, mSlurp..mQuoteAst:
1842+
of mNLen..mNError, mStatic..mQuoteAst:
18431843
localReport(p.config, e.info, reportSym(
18441844
rsemConstExpressionExpected, e[0].sym))
18451845

compiler/backend/jsgen.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,7 @@ proc genMagic(p: PProc, n: CgNode, r: var TCompRes) =
20092009
of mDefault: genDefault(p, n, r)
20102010
of mWasMoved: genReset(p, n)
20112011
of mEcho: genEcho(p, n, r)
2012-
of mNLen..mNError, mSlurp:
2012+
of mNLen..mNError:
20132013
localReport(p.config, n.info, reportSym(
20142014
rsemConstExpressionExpected, n[0].sym))
20152015

compiler/front/cli_reporter.nim

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,15 +2632,6 @@ To create a stacktrace, rerun compilation with './koch temp $1 <file>'
26322632
of rintCannotOpenFile, rintWarnCannotOpenFile:
26332633
result = "cannot open file: $1" % r.file
26342634

2635-
of rintUnknown:
2636-
result = "unknown"
2637-
2638-
of rintFatal:
2639-
result = "fatal"
2640-
2641-
of rintIce:
2642-
result = r.msg
2643-
26442635
of rintNotUsingNimcore:
26452636
result = "Nim tooling must be built using -d:nimcore"
26462637

@@ -3990,17 +3981,8 @@ proc reportHook*(conf: ConfigRef, r: Report): TErrorHandling =
39903981
## `reportBody` for report, which then calls respective (for each report
39913982
## category) `reportBody` overloads defined above
39923983
assertKind r
3993-
let wkind = conf.writabilityKind(r)
3994-
# debug reports can be both enabled and force enabled. So adding a case here
3995-
# is not really useful, since report writability kind does not necessarily
3996-
# dictate how it is written, just whether it can/must/cannot be written.
3997-
# xxx: the above convoluted comment brought to you by _glaring_ design flaws!
3998-
if wkind == writeDisabled:
3999-
return
4000-
else:
4001-
if wkind == writeForceEnabled:
4002-
echo conf.reportFull(r)
4003-
elif r.kind == rsemProcessing and conf.hintProcessingDots:
3984+
if true:
3985+
if r.kind == rsemProcessing and conf.hintProcessingDots:
40043986
# xxx: the report hook is handling processing dots output, why? this whole
40053987
# infrastructure is overwrought. seriously, they're not hints, they're
40063988
# progress indicators.

compiler/front/msgs.nim

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ proc report*(conf: ConfigRef, node: PNode): TErrorHandling =
609609

610610
proc fillReportAndHandleVmTrace(c: ConfigRef, r: var Report,
611611
reportFrom: InstantiationInfo) =
612-
r.reportFrom = toReportLineInfo(reportFrom)
613612
if r.category in { repSem, repVM } and r.location.isSome():
614613
r.context = c.getContext(r.location.get())
615614

@@ -621,24 +620,63 @@ proc handleReport*(
621620
r: Report,
622621
reportFrom: InstantiationInfo,
623622
eh: TErrorHandling = doNothing) {.noinline.} =
623+
## Takes the report `r` and handles it. If the report is "enabled" according
624+
## to the active configuration, it is passed to the active report hook and,
625+
## if the report corresponds to an error, error handling is performed.
626+
## `eh` is currently only a suggestion, and it is sometimes ignored depending
627+
## on the currently active configuration.
624628
var rep = r
625-
fillReportAndHandleVmTrace(conf, rep, reportFrom)
629+
rep.reportFrom = toReportLineInfo(reportFrom)
630+
631+
if not conf.isEnabled(rep):
632+
# the report is disabled -> neither invoke the report hook nor perform
633+
# error handling
634+
return
635+
636+
var userAction = doNothing
637+
case writabilityKind(conf, rep)
638+
of writeDisabled:
639+
discard "don't invoke the hook"
640+
of writeEnabled:
641+
# go through the report hook
642+
fillReportAndHandleVmTrace(conf, rep, reportFrom)
643+
userAction = conf.report(rep)
644+
of writeForceEnabled:
645+
# also go through the report hook, but temporarily override ``writeln``
646+
# with something that always echoes something
647+
fillReportAndHandleVmTrace(conf, rep, reportFrom)
648+
let oldHook = conf.writelnHook
649+
conf.writelnHook = proc (conf: ConfigRef, msg: string, flags: MsgFlags) =
650+
echo msg
626651

627-
let
628652
userAction = conf.report(rep)
629-
(action, trace) =
630-
case userAction
631-
of doDefault:
632-
errorActions(conf, rep, eh)
633-
else:
634-
(userAction, false)
653+
conf.writelnHook = oldHook
635654

655+
# ``errorActions`` also increments the error counter, so make sure to always
656+
# call it
657+
var (action, trace) = errorActions(conf, rep, eh)
658+
659+
# decide what to do, based on the hook-provided action and the computed
660+
# action. The more severe handling out of the two wins
661+
case userAction
662+
of doAbort:
663+
# a hook-requested abort always overrides the computed handling
664+
(action, trace) = (doAbort, false)
665+
of doRaise:
666+
case action
667+
of doRaise, doAbort:
668+
discard "a hook-requested raise doesn't override an abort"
669+
of doNothing, doDefault:
670+
(action, trace) = (doRaise, false)
671+
of doNothing, doDefault:
672+
discard "use the computed strategy"
673+
674+
# now perform the selected action:
636675
case action
637676
of doAbort: quit(conf, trace)
638677
of doRaise: raiseRecoverableError("report")
639678
of doNothing: discard
640-
of doDefault: assert(
641-
false,
679+
of doDefault: unreachable(
642680
"Default error handing action must be turned into ignore/raise/abort")
643681

644682
template globalAssert*(
@@ -665,34 +703,6 @@ template globalReport*(conf: ConfigRef, report: ReportTypes) =
665703
handleReport(
666704
conf, wrap(report, instLoc()), instLoc(), doRaise)
667705

668-
proc reportAndFail*(
669-
conf: ConfigRef, r: Report, reportFrom: InstantiationInfo) =
670-
## Similar to `handleReport`, but, unless overridden with aborting
671-
## (`doAbort`) by the structured report hook, always raises a recoverable
672-
## error.
673-
var rep = r
674-
fillReportAndHandleVmTrace(conf, rep, reportFrom)
675-
676-
case conf.report(rep)
677-
of doAbort:
678-
quit(conf, false)
679-
of doDefault:
680-
let (action, trace) = errorActions(conf, rep, doRaise)
681-
case action
682-
of doAbort:
683-
quit(conf, trace)
684-
of doRaise, doNothing:
685-
raiseRecoverableError("report")
686-
of doDefault:
687-
unreachable()
688-
of doRaise, doNothing:
689-
raiseRecoverableError("report")
690-
691-
template reportAndFail*(
692-
conf: ConfigRef; info: TLineInfo, report: ReportTypes) =
693-
reportAndFail(
694-
conf, wrap(report, instLoc(), info), instLoc())
695-
696706
template localReport*(conf: ConfigRef; info: TLineInfo, report: ReportTypes) =
697707
{.line.}:
698708
handleReport(
@@ -717,27 +727,9 @@ template localReport*(conf: ConfigRef, report: Report) =
717727

718728
# xxx: `internalError` and `internalAssert` in conjunction with `handleReport`,
719729
# and the whole concept of "reports" indicating error handling action at a
720-
# callsite, is *terrible*. Since neither will necessarily raise/end
721-
# execution of the current routine, which may lead to NPEs and the like.
722-
723-
template internalError*(
724-
conf: ConfigRef, repKind: InternalReportKind, fail: string): untyped =
725-
## Causes an internal error; but does not necessarily raise/end the currently
726-
## executing routine.
727-
conf.handleReport(
728-
wrap(InternalReport(kind: repKind, msg: fail), instLoc()),
729-
instLoc(),
730-
doAbort)
731-
732-
template internalError*(
733-
conf: ConfigRef, info: TLineInfo,
734-
repKind: InternalReportKind, fail: string): untyped =
735-
## Causes an internal error; but does not necessarily raise/end the currently
736-
## executing routine.
737-
conf.handleReport(
738-
wrap(InternalReport(kind: repKind, msg: fail), instLoc(), info),
739-
instLoc(),
740-
doAbort)
730+
# callsite, is *terrible*. While it will result in the compiler exiting,
731+
# it is currently implemented very indirectly, through
732+
# ``isCompilerFatal``.
741733

742734
proc doInternalUnreachable*(conf: ConfigRef, info: TLineInfo, msg: string,
743735
instLoc: InstantiationInfo) {.noreturn, inline.} =
@@ -752,19 +744,18 @@ proc doInternalUnreachable*(conf: ConfigRef, info: TLineInfo, msg: string,
752744
wrap(intRep, instLoc, info)
753745

754746
conf.handleReport(rep, instLoc, doAbort)
747+
unreachable("not aborted")
755748

756749
template internalError*(
757750
conf: ConfigRef,
758751
info: TLineInfo,
759752
fail: string,
760753
): untyped =
761-
## Causes an internal error; but does not necessarily raise/end the currently
762-
## executing routine.
754+
## Causes an internal error. Always ends the currently executing routine.
763755
doInternalUnreachable(conf, info, fail, instLoc())
764756

765757
template internalError*(conf: ConfigRef, fail: string): untyped =
766-
## Causes an internal error; but does not necessarily raise/end the currently
767-
## executing routine.
758+
## Causes an internal error. Always ends the currently executing routine.
768759
doInternalUnreachable(conf, unknownLineInfo, fail, instLoc())
769760

770761
proc doInternalAssert*(conf: ConfigRef,
@@ -782,17 +773,18 @@ proc doInternalAssert*(conf: ConfigRef,
782773
wrap(intRep, instLoc, info)
783774

784775
conf.handleReport(rep, instLoc, doAbort)
776+
unreachable("not aborted")
785777

786778
template internalAssert*(
787779
conf: ConfigRef, condition: bool, info: TLineInfo, failMsg: string = "") =
788-
## Causes an internal error if the provided condition evaluates to false; but
789-
## does not necessarily raise/end the currently executing routine.
780+
## Causes an internal error if the provided condition evaluates to false.
781+
## Always ends the currently executing routine.
790782
if not condition:
791783
doInternalAssert(conf, instLoc(), failMsg, info)
792784

793785
template internalAssert*(conf: ConfigRef, condition: bool, failMsg = "") =
794-
## Causes an internal error if the provided condition evaluates to false; but
795-
## does not necessarily raise/end the currently executing routine.
786+
## Causes an internal error if the provided condition evaluates to false.
787+
## Always ends the currently executing routine.
796788
if not condition:
797789
doInternalAssert(conf, instLoc(), failMsg)
798790

0 commit comments

Comments
 (0)