Skip to content

Commit c063984

Browse files
timotheecourardek66
authored andcommitted
fix hintProcessing dots interference with static:echo and hintCC; add tests for nim secret, add tests for hintProcessing, misc other bug fixes (nim-lang#16495)
* fix dots interfering with static:echo * add tests * fix hintProcessing dots for hintCC * improve trunner tests * fix bug: readLineFromStdin now writes prompt to stdout, consistent with linenoise and rdstdin * disable a failing test for windows
1 parent cb858a5 commit c063984

File tree

7 files changed

+77
-21
lines changed

7 files changed

+77
-21
lines changed

compiler/extccomp.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,10 @@ proc callCCompiler*(conf: ConfigRef) =
848848
var cmds: TStringSeq
849849
var prettyCmds: TStringSeq
850850
let prettyCb = proc (idx: int) =
851-
if prettyCmds[idx].len > 0: echo prettyCmds[idx]
851+
if prettyCmds[idx].len > 0:
852+
flushDot(conf)
853+
# xxx should probably use stderr like other compiler messages, not stdout
854+
echo prettyCmds[idx]
852855

853856
for idx, it in conf.toCompile:
854857
# call the C compiler for the .c file:

compiler/llstream.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ proc llStreamClose*(s: PLLStream) =
7575
when not declared(readLineFromStdin):
7676
# fallback implementation:
7777
proc readLineFromStdin(prompt: string, line: var string): bool =
78-
stderr.write(prompt)
78+
stdout.write(prompt)
7979
result = readLine(stdin, line)
8080
if not result:
81-
stderr.write("\n")
81+
stdout.write("\n")
8282
quit(0)
8383

8484
proc endsWith*(x: string, s: set[char]): bool =

compiler/main.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ proc commandInteractive(graph: ModuleGraph) =
138138
var m = graph.makeStdinModule()
139139
incl(m.flags, sfMainModule)
140140
var idgen = IdGenerator(module: m.itemId.module, item: m.itemId.item)
141-
let s = llStreamOpenStdIn(onPrompt = proc() = flushDot(graph.config, stderr))
141+
let s = llStreamOpenStdIn(onPrompt = proc() = flushDot(graph.config))
142142
processModule(graph, m, idgen, s)
143143

144144
proc commandScan(cache: IdentCache, config: ConfigRef) =

compiler/msgs.nim

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ template instLoc(): InstantiationInfo = instantiationInfo(-2, fullPaths = true)
1919
template toStdOrrKind(stdOrr): untyped =
2020
if stdOrr == stdout: stdOrrStdout else: stdOrrStderr
2121

22-
template flushDot*(conf, stdOrr) =
22+
proc flushDot*(conf: ConfigRef) =
2323
## safe to call multiple times
24+
# xxx one edge case not yet handled is when `printf` is called at CT with `compiletimeFFI`.
25+
let stdOrr = if optStdout in conf.globalOptions: stdout else: stderr
2426
let stdOrrKind = toStdOrrKind(stdOrr)
2527
if stdOrrKind in conf.lastMsgWasDot:
2628
conf.lastMsgWasDot.excl stdOrrKind
@@ -311,12 +313,12 @@ proc msgWriteln*(conf: ConfigRef; s: string, flags: MsgFlags = {}) =
311313
conf.writelnHook(s)
312314
elif optStdout in conf.globalOptions or msgStdout in flags:
313315
if eStdOut in conf.m.errorOutputs:
314-
flushDot(conf, stdout)
316+
flushDot(conf)
315317
writeLine(stdout, s)
316318
flushFile(stdout)
317319
else:
318320
if eStdErr in conf.m.errorOutputs:
319-
flushDot(conf, stderr)
321+
flushDot(conf)
320322
writeLine(stderr, s)
321323
# On Windows stderr is fully-buffered when piped, regardless of C std.
322324
when defined(windows):
@@ -368,11 +370,11 @@ template styledMsgWriteln*(args: varargs[typed]) =
368370
callIgnoringStyle(callWritelnHook, nil, args)
369371
elif optStdout in conf.globalOptions:
370372
if eStdOut in conf.m.errorOutputs:
371-
flushDot(conf, stdout)
373+
flushDot(conf)
372374
callIgnoringStyle(writeLine, stdout, args)
373375
flushFile(stdout)
374376
elif eStdErr in conf.m.errorOutputs:
375-
flushDot(conf, stderr)
377+
flushDot(conf)
376378
if optUseColors in conf.globalOptions:
377379
callStyledWriteLineStderr(args)
378380
else:

compiler/options.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,11 @@ proc setNote*(conf: ConfigRef, note: TNoteKind, enabled = true) =
368368
if enabled: incl(conf.notes, note) else: excl(conf.notes, note)
369369

370370
proc hasHint*(conf: ConfigRef, note: TNoteKind): bool =
371+
# ternary states instead of binary states would simplify logic
371372
if optHints notin conf.options: false
372-
elif note in {hintConf}: # could add here other special notes like hintSource
373+
elif note in {hintConf, hintProcessing}:
374+
# could add here other special notes like hintSource
375+
# these notes apply globally.
373376
note in conf.mainPackageNotes
374377
else: note in conf.notes
375378

compiler/vm.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,14 +1153,14 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
11531153
stackTrace(c, tos, pc, "node is not a proc symbol")
11541154
of opcEcho:
11551155
let rb = instr.regB
1156-
if rb == 1:
1157-
msgWriteln(c.config, regs[ra].node.strVal, {msgStdout})
1156+
template fn(s) = msgWriteln(c.config, s, {msgStdout})
1157+
if rb == 1: fn(regs[ra].node.strVal)
11581158
else:
11591159
var outp = ""
11601160
for i in ra..ra+rb-1:
11611161
#if regs[i].kind != rkNode: debug regs[i]
11621162
outp.add(regs[i].node.strVal)
1163-
msgWriteln(c.config, outp, {msgStdout})
1163+
fn(outp)
11641164
of opcContainsSet:
11651165
decodeBC(rkInt)
11661166
regs[ra].intVal = ord(inSet(regs[rb].node, regs[rc].regToNode))

tests/misc/trunner.nim

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ from std/sequtils import toSeq,mapIt
1212
from std/algorithm import sorted
1313
import stdtest/[specialpaths, unittest_light]
1414
from std/private/globs import nativeToUnixPath
15-
15+
from strutils import startsWith, strip, removePrefix
16+
from std/sugar import dup
1617
import "$lib/../compiler/nimpaths"
1718

19+
proc isDots(a: string): bool =
20+
## test for `hintProcessing` dots
21+
a.startsWith(".") and a.strip(chars = {'.'}) == ""
22+
1823
const
24+
defaultHintsOff = "--hint:successx:off --hint:exec:off --hint:link:off --hint:cc:off --hint:conf:off --hint:processing:off --hint:QuitCalled:off"
25+
# useful when you want to turn only some hints on, and some common ones off.
26+
# pending https://github.com/timotheecour/Nim/issues/453, simplify to: `--hints:off`
1927
nim = getCurrentCompilerExe()
2028
mode =
2129
when defined(c): "c"
@@ -93,10 +101,9 @@ else: # don't run twice the same test
93101
check exitCode == 0
94102
let ret = toSeq(walkDirRec(htmldocsDir, relative=true)).mapIt(it.nativeToUnixPath).sorted.join("\n")
95103
let context = $(i, ret, cmd)
96-
var expected = ""
97104
case i
98105
of 0,5:
99-
let htmlFile = htmldocsDir/"mmain.html"
106+
let htmlFile = htmldocsDir/mainFname
100107
check htmlFile in outp # sanity check for `hintSuccessX`
101108
assertEquals ret, fmt"""
102109
{dotdotMangle}/imp.html
@@ -106,7 +113,7 @@ imp.html
106113
imp.idx
107114
imp2.html
108115
imp2.idx
109-
mmain.html
116+
{mainFname}
110117
mmain.idx
111118
{nimdocOutCss}
112119
{theindexFname}""", context
@@ -119,21 +126,21 @@ tests/nimdoc/sub/imp.html
119126
tests/nimdoc/sub/imp.idx
120127
tests/nimdoc/sub/imp2.html
121128
tests/nimdoc/sub/imp2.idx
122-
tests/nimdoc/sub/mmain.html
129+
tests/nimdoc/sub/{mainFname}
123130
tests/nimdoc/sub/mmain.idx
124131
{theindexFname}"""
125132
of 2, 3: assertEquals ret, fmt"""
126133
{docHackJsFname}
127-
mmain.html
134+
{mainFname}
128135
mmain.idx
129136
{nimdocOutCss}""", context
130137
of 4: assertEquals ret, fmt"""
131138
{docHackJsFname}
132139
{nimdocOutCss}
133-
sub/mmain.html
140+
sub/{mainFname}
134141
sub/mmain.idx""", context
135142
of 6: assertEquals ret, fmt"""
136-
mmain.html
143+
{mainFname}
137144
{nimdocOutCss}""", context
138145
else: doAssert false
139146

@@ -222,3 +229,44 @@ mmain.html
222229
check fmt"""{nim} {opt} --eval:"echo defined(nimscript)"""".execCmdEx == ("true\n", 0)
223230
check fmt"""{nim} r {opt} --eval:"echo defined(c)"""".execCmdEx == ("true\n", 0)
224231
check fmt"""{nim} r -b:js {opt} --eval:"echo defined(js)"""".execCmdEx == ("true\n", 0)
232+
233+
block: # `hintProcessing` dots should not interfere with `static: echo` + friends
234+
let cmd = fmt"""{nim} r {defaultHintsOff} --hint:processing -f --eval:"static: echo 1+1""""
235+
let (outp, exitCode) = execCmdEx(cmd, options = {poStdErrToStdOut})
236+
template check3(cond) = doAssert cond, $(outp,)
237+
doAssert exitCode == 0
238+
let lines = outp.splitLines
239+
check3 lines.len == 3
240+
when not defined(windows): # xxx: on windows, dots not properly handled, gives: `....2\n\n`
241+
check3 lines[0].isDots
242+
check3 lines[1] == "2"
243+
check3 lines[2] == ""
244+
else:
245+
check3 "2" in outp
246+
247+
block: # nim secret
248+
let opt = fmt"{defaultHintsOff} --hint:processing"
249+
template check3(cond) = doAssert cond, $(outp,)
250+
for extra in ["", "--stdout"]:
251+
let cmd = fmt"""{nim} secret {opt} {extra}"""
252+
# xxx minor bug: `nim --hint:QuitCalled:off secret` ignores the hint cmdline flag
253+
template run(input2): untyped =
254+
execCmdEx(cmd, options = {poStdErrToStdOut}, input = input2)
255+
block:
256+
let (outp, exitCode) = run """echo 1+2; import strutils; echo strip(" ab "); quit()"""
257+
let lines = outp.splitLines
258+
when not defined(windows):
259+
check3 lines.len == 5
260+
check3 lines[0].isDots
261+
check3 lines[1].dup(removePrefix(">>> ")) == "3" # prompt depends on `nimUseLinenoise`
262+
check3 lines[2].isDots
263+
check3 lines[3] == "ab"
264+
check3 lines[4] == ""
265+
else:
266+
check3 "3" in outp
267+
check3 "ab" in outp
268+
doAssert exitCode == 0
269+
block:
270+
let (outp, exitCode) = run "echo 1+2; quit(2)"
271+
check3 "3" in outp
272+
doAssert exitCode == 2

0 commit comments

Comments
 (0)