Skip to content

Commit 9d26c26

Browse files
authored
Emit compiler warning when Var and local names are shadowed (#303)
* Emit compiler warning when Var and local names are shadowed * Tests
1 parent e191b47 commit 9d26c26

File tree

4 files changed

+322
-92
lines changed

4 files changed

+322
-92
lines changed

src/basilisp/cli.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,28 @@ def bootstrap_repl(which_ns: str) -> types.ModuleType:
5151

5252
@cli.command(short_help='start the Basilisp REPL')
5353
@click.option('--default-ns', default=runtime._REPL_DEFAULT_NS, help='default namespace to use for the REPL')
54-
def repl(default_ns):
54+
@click.option('--use-var-indirection',
55+
default=False,
56+
is_flag=True,
57+
envvar='BASILISP_USE_VAR_INDIRECTION',
58+
help='if provided, all Var accesses will be performed via Var indirection')
59+
@click.option('--warn-on-shadowed-name',
60+
default=False,
61+
is_flag=True,
62+
envvar='BASILISP_WARN_ON_SHADOWED_NAME',
63+
help='if provided, emit warnings if a local name is shadowed by another local name')
64+
@click.option('--warn-on-shadowed-var',
65+
default=True,
66+
is_flag=True,
67+
envvar='BASILISP_WARN_ON_SHADOWED_VAR',
68+
help='if provided, emit warnings if a Var name is shadowed by a local name')
69+
def repl(default_ns, use_var_indirection, warn_on_shadowed_name, warn_on_shadowed_var):
5570
basilisp.init()
5671
repl_module = bootstrap_repl(default_ns)
57-
ctx = compiler.CompilerContext()
72+
ctx = compiler.CompilerContext(
73+
{compiler.USE_VAR_INDIRECTION: use_var_indirection,
74+
compiler.WARN_ON_SHADOWED_NAME: warn_on_shadowed_name,
75+
compiler.WARN_ON_SHADOWED_VAR: warn_on_shadowed_var})
5876
ns_var = runtime.set_current_ns(default_ns)
5977
eof = object()
6078
while True:
@@ -95,10 +113,33 @@ def repl(default_ns):
95113
@click.argument('file-or-code')
96114
@click.option('-c', '--code', is_flag=True, help='if provided, treat argument as a string of code')
97115
@click.option('--in-ns', default=runtime._REPL_DEFAULT_NS, help='namespace to use for the code')
98-
def run(file_or_code, code, in_ns):
116+
@click.option('--use-var-indirection',
117+
default=False,
118+
is_flag=True,
119+
envvar='BASILISP_USE_VAR_INDIRECTION',
120+
help='if provided, all Var accesses will be performed via Var indirection')
121+
@click.option('--warn-on-shadowed-name',
122+
default=False,
123+
is_flag=True,
124+
envvar='BASILISP_WARN_ON_SHADOWED_NAME',
125+
help='if provided, emit warnings if a local name is shadowed by another local name')
126+
@click.option('--warn-on-shadowed-var',
127+
default=True,
128+
is_flag=True,
129+
envvar='BASILISP_WARN_ON_SHADOWED_VAR',
130+
help='if provided, emit warnings if a Var name is shadowed by a local name')
131+
def run(file_or_code, # pylint: disable=too-many-arguments
132+
code,
133+
in_ns,
134+
use_var_indirection,
135+
warn_on_shadowed_name,
136+
warn_on_shadowed_var):
99137
"""Run a Basilisp script or a line of code, if it is provided."""
100138
basilisp.init()
101-
ctx = compiler.CompilerContext()
139+
ctx = compiler.CompilerContext(
140+
{compiler.USE_VAR_INDIRECTION: use_var_indirection,
141+
compiler.WARN_ON_SHADOWED_NAME: warn_on_shadowed_name,
142+
compiler.WARN_ON_SHADOWED_VAR: warn_on_shadowed_var})
102143
eof = object()
103144

104145
with runtime.ns_bindings(in_ns) as ns:

0 commit comments

Comments
 (0)