From e290b028aba9fd50a69ed8c0346ee323f9287a13 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 11 Oct 2022 00:56:01 +0800 Subject: [PATCH] Make rstgen work with gcsafe (#20534) * Make rstgen work with gcsafe Co-authored-by: Danil Yarantsev * add tests and fixes * if nimHasWarningAsError Co-authored-by: Danil Yarantsev --- compiler/docgen.nim | 6 +++--- compiler/msgs.nim | 2 +- compiler/nim.cfg | 4 ++++ compiler/options.nim | 2 +- lib/packages/docutils/rstgen.nim | 8 ++++---- tests/stdlib/trstgen.nim | 5 +++++ 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 41c898553..5ba46c80b 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -333,7 +333,7 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef, result.jEntriesFinal = newJArray() initStrTable result.types result.onTestSnippet = - proc (gen: var RstGenerator; filename, cmd: string; status: int; content: string) = + proc (gen: var RstGenerator; filename, cmd: string; status: int; content: string) {.gcsafe.} = if conf.docCmd == docCmdSkip: return inc(gen.id) var d = (ptr TDocumentor)(addr gen) @@ -1732,7 +1732,7 @@ proc commandJson*(cache: IdentCache, conf: ConfigRef) = if ast == nil: return var d = newDocumentor(conf.projectFull, cache, conf, hasToc = true) d.onTestSnippet = proc (d: var RstGenerator; filename, cmd: string; - status: int; content: string) = + status: int; content: string) {.gcsafe.} = localError(conf, newLineInfo(conf, AbsoluteFile d.filename, -1, -1), warnUser, "the ':test:' attribute is not supported by this backend") generateJson(d, ast) @@ -1755,7 +1755,7 @@ proc commandTags*(cache: IdentCache, conf: ConfigRef) = if ast == nil: return var d = newDocumentor(conf.projectFull, cache, conf, hasToc = true) d.onTestSnippet = proc (d: var RstGenerator; filename, cmd: string; - status: int; content: string) = + status: int; content: string) {.gcsafe.} = localError(conf, newLineInfo(conf, AbsoluteFile d.filename, -1, -1), warnUser, "the ':test:' attribute is not supported by this backend") var diff --git a/compiler/msgs.nim b/compiler/msgs.nim index c7610b630..c0362693f 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -504,7 +504,7 @@ proc formatMsg*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string): s conf.toFileLineCol(info) & " " & title & getMessageStr(msg, arg) proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string, - eh: TErrorHandling, info2: InstantiationInfo, isRaw = false) {.noinline.} = + eh: TErrorHandling, info2: InstantiationInfo, isRaw = false) {.gcsafe, noinline.} = var title: string color: ForegroundColor diff --git a/compiler/nim.cfg b/compiler/nim.cfg index 5156fa971..020104fe5 100644 --- a/compiler/nim.cfg +++ b/compiler/nim.cfg @@ -34,3 +34,7 @@ define:useStdoutAsStdmsg experimental:strictEffects warningAsError:Effect:on @end + +@if nimHasWarningAsError: + warningAsError:GcUnsafe2:on +@end diff --git a/compiler/options.nim b/compiler/options.nim index 1b352984f..6257f8969 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -395,7 +395,7 @@ type suggestVersion*: int suggestMaxResults*: int lastLineInfo*: TLineInfo - writelnHook*: proc (output: string) {.closure.} # cannot make this gcsafe yet because of Nimble + writelnHook*: proc (output: string) {.closure, gcsafe.} structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string; severity: Severity) {.closure, gcsafe.} cppCustomNamespace*: string diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index d1f2a7511..bd1137789 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -88,7 +88,7 @@ type ## for hyperlinks. See renderIndexTerm proc for details. id*: int ## A counter useful for generating IDs. onTestSnippet*: proc (d: var RstGenerator; filename, cmd: string; status: int; - content: string) + content: string) {.gcsafe.} escMode*: EscapeMode curQuotationDepth: int @@ -283,7 +283,7 @@ proc dispA(target: OutputTarget, dest: var string, proc `or`(x, y: string): string {.inline.} = result = if x.len == 0: y else: x -proc renderRstToOut*(d: var RstGenerator, n: PRstNode, result: var string) +proc renderRstToOut*(d: var RstGenerator, n: PRstNode, result: var string) {.gcsafe.} ## Writes into ``result`` the rst ast ``n`` using the ``d`` configuration. ## ## Before using this proc you need to initialise a ``RstGenerator`` with @@ -1028,7 +1028,7 @@ proc renderCodeLang*(result: var string, lang: SourceLanguage, code: string, proc renderNimCode*(result: var string, code: string, target: OutputTarget) = renderCodeLang(result, langNim, code, target) -proc renderCode(d: PDoc, n: PRstNode, result: var string) = +proc renderCode(d: PDoc, n: PRstNode, result: var string) {.gcsafe.} = ## Renders a code (code block or inline code), appending it to `result`. ## ## If the code block uses the ``number-lines`` option, a table will be @@ -1592,7 +1592,7 @@ $content proc rstToHtml*(s: string, options: RstParseOptions, config: StringTableRef, - msgHandler: MsgHandler = rst.defaultMsgHandler): string = + msgHandler: MsgHandler = rst.defaultMsgHandler): string {.gcsafe.} = ## Converts an input rst string into embeddable HTML. ## ## This convenience proc parses any input string using rst markup (it doesn't diff --git a/tests/stdlib/trstgen.nim b/tests/stdlib/trstgen.nim index a25d903db..8c07b35f0 100644 --- a/tests/stdlib/trstgen.nim +++ b/tests/stdlib/trstgen.nim @@ -1684,3 +1684,8 @@ suite "local file inclusion": discard "```nim file = ./readme.md\n```".toHtml(error=error) check(error[] == "input(1, 23) Error: disabled directive: 'file'") +proc documentToHtml*(doc: string, isMarkdown: bool = false): string {.gcsafe.} = + var options = {roSupportMarkdown} + if isMarkdown: + options.incl roPreferMarkdown + result = rstToHtml(doc, options, defaultConfig())