Skip to content

Commit 87ee9c8

Browse files
authored
makes DuplicateModuleImport back to an error (#25178)
fixes #24998 Basically it retraces back to the situation before #18366 and #18362, i.e. ```nim import fuzz/a import fuzz/a ``` ```nim import fuzz/a from buzz/a ``` ```nim import fuzz/a except nil from fuzz/a import addInt ``` All of these cases are now flagged as invalid and triggers a redefinition error, i.e., each module name importing is treated as consistent as the symbol definition kinda annoying for importing/exporting with `when conditions` though ref #18762 #20907 ```nim from std/strutils import toLower when not defined(js): from std/strutils import toUpper ```
1 parent 16394c3 commit 87ee9c8

File tree

8 files changed

+9
-12
lines changed

8 files changed

+9
-12
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ errors.
2525

2626
- `std/parsesql` has been moved to a nimble package, use `nimble` or `atlas` to install it.
2727

28+
- With `-d:nimPreviewDuplicateModuleError`, importing two modules that share the same name becomes a compile-time error. This includes importing the same module more than once. Use `import foo as foo1` (or other aliases) to avoid collisions.
29+
2830
## Standard library additions and changes
2931

3032
[//]: # "Additions:"

compiler/condsyms.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,5 @@ proc initDefines*(symbols: StringTableRef) =
173173
defineSymbol("nimHasXorSet")
174174

175175
defineSymbol("nimHasSetLengthSeqUninitMagic")
176+
defineSymbol("nimHasPreviewDuplicateModuleError")
176177

compiler/lookups.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ proc addDeclAt*(c: PContext; scope: PScope, sym: PSym, info: TLineInfo) =
388388
if sym.name.id == ord(wUnderscore): return
389389
let conflict = scope.addUniqueSym(sym)
390390
if conflict != nil:
391-
if sym.kind == skModule and conflict.kind == skModule:
391+
if sym.kind == skModule and conflict.kind == skModule and not c.config.isDefined("nimPreviewDuplicateModuleError"):
392392
# e.g.: import foo; import foo
393393
# xxx we could refine this by issuing a different hint for the case
394394
# where a duplicate import happens inside an include.

compiler/nim.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ define:nimPreviewNonVarDestructor
1212
define:nimPreviewCheckedClose
1313
define:nimPreviewAsmSemSymbol
1414
define:nimPreviewCStringComparisons
15+
define:nimPreviewDuplicateModuleError
16+
1517
threads:off
1618

1719
#import:"$projectpath/testability"

compiler/suggest.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import prefixmatches, suggestsymdb
3636
from wordrecg import wDeprecated, wError, wAddr, wYield
3737

38-
import std/[algorithm, sets, parseutils, tables, os]
38+
import std/[algorithm, sets, parseutils, os]
3939

4040
when defined(nimsuggest):
4141
import pathutils # importer

lib/pure/terminal.nim

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const
100100
stylePrefix = "\e["
101101

102102
when defined(windows):
103-
import std/[winlean, os]
103+
import std/os
104104

105105
const
106106
DUPLICATE_SAME_ACCESS = 2
@@ -926,8 +926,6 @@ when defined(windows):
926926
stdout.write "\n"
927927

928928
else:
929-
import std/termios
930-
931929
proc readPasswordFromStdin*(prompt: string, password: var string):
932930
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
933931
password.setLen(0)
@@ -981,9 +979,6 @@ proc isTrueColorSupported*(): bool =
981979
## Returns true if a terminal supports true color.
982980
return getTerminal().trueColorIsSupported
983981

984-
when defined(windows):
985-
import std/os
986-
987982
proc enableTrueColors*() =
988983
## Enables true color.
989984
var term = getTerminal()

lib/system/alloc.nim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
include osalloc
1414
import std/private/syslocks
15-
import std/sysatomics
1615

1716
template track(op, address, size) =
1817
when defined(memTracker):

lib/system/strmantle.nim

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
#
99

1010
# Compilerprocs for strings that do not depend on the string implementation.
11-
12-
import std/private/digitsutils
13-
11+
import std/private/digitsutils as digitsutils2
1412

1513
proc cmpStrings(a, b: string): int {.inline, compilerproc.} =
1614
let alen = a.len

0 commit comments

Comments
 (0)