Skip to content

Commit adaefb3

Browse files
authored
Fix an erroneous compiler warning on Var redef (#917)
Fixes #916
1 parent 95e30df commit adaefb3

File tree

4 files changed

+305
-186
lines changed

4 files changed

+305
-186
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
* Fix a bug where nested quotation marks were not escaped properly by various print functions and at the REPL (#894)
1818
* Fix a bug that caused a syntax error when presenting any filepath that includes the MS-Windows `\` file separator to the cli run command (#912)
1919
* Fix a bug where the core functions `symbol` and `keyword` would not accept non-string data types (#911)
20+
* Fix a bug where the compiler would emit warnings on when a Var was redef'ed even if that Var was initially defined with `^:redef` metadata (#916)
2021

2122
### Other
2223
* Update Sphinx documentation theme (#909)

src/basilisp/lang/compiler/analyzer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@
6666
SYM_MACRO_META_KEY,
6767
SYM_MUTABLE_META_KEY,
6868
SYM_NO_INLINE_META_KEY,
69+
SYM_NO_WARN_ON_REDEF_META_KEY,
6970
SYM_NO_WARN_ON_SHADOW_META_KEY,
7071
SYM_NO_WARN_WHEN_UNUSED_META_KEY,
7172
SYM_PRIVATE_META_KEY,
7273
SYM_PROPERTY_META_KEY,
74+
SYM_REDEF_META_KEY,
7375
SYM_STATICMETHOD_META_KEY,
7476
SYM_TAG_META_KEY,
7577
VAR_IS_PROTOCOL_META_KEY,
@@ -899,6 +901,27 @@ def _await_ast(form: ISeq, ctx: AnalyzerContext) -> Await:
899901
)
900902

901903

904+
def __should_warn_on_redef(
905+
current_ns: runtime.Namespace,
906+
defsym: sym.Symbol,
907+
def_meta: Optional[lmap.PersistentMap],
908+
) -> bool:
909+
"""Return True if the compiler should emit a warning about this name being redefined."""
910+
if def_meta is not None and def_meta.val_at(SYM_NO_WARN_ON_REDEF_META_KEY, False):
911+
return False
912+
913+
if defsym not in current_ns.interns:
914+
return False
915+
916+
var = current_ns.find(defsym)
917+
assert var is not None, f"Var {defsym} cannot be none here"
918+
919+
if var.meta is not None and var.meta.val_at(SYM_REDEF_META_KEY):
920+
return False
921+
else:
922+
return bool(var.is_bound)
923+
924+
902925
def _def_ast( # pylint: disable=too-many-locals,too-many-statements
903926
form: ISeq, ctx: AnalyzerContext
904927
) -> Def:
@@ -996,6 +1019,13 @@ def _def_ast( # pylint: disable=too-many-locals,too-many-statements
9961019
# its meta may contain values which fail to compile.
9971020
bare_name = sym.symbol(name.name)
9981021

1022+
# Warn if this symbol is potentially being redefined (if the Var was
1023+
# previously bound)
1024+
if __should_warn_on_redef(current_ns, bare_name, def_meta):
1025+
logger.warning(
1026+
f"redefining Var '{bare_name}' in namespace {current_ns}:{def_loc[0]}"
1027+
)
1028+
9991029
ns_sym = sym.symbol(current_ns.name)
10001030
var = Var.intern_unbound(
10011031
ns_sym,

src/basilisp/lang/compiler/generator.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
INTERFACE_KW,
4949
REST_KW,
5050
SYM_DYNAMIC_META_KEY,
51-
SYM_NO_WARN_ON_REDEF_META_KEY,
5251
SYM_REDEF_META_KEY,
5352
VAR_IS_PROTOCOL_META_KEY,
5453
)
@@ -827,30 +826,6 @@ def _await_to_py_ast(ctx: GeneratorContext, node: Await) -> GeneratedPyAST[ast.e
827826
)
828827

829828

830-
def __should_warn_on_redef(
831-
ctx: GeneratorContext,
832-
defsym: sym.Symbol,
833-
safe_name: str,
834-
def_meta: lmap.PersistentMap,
835-
) -> bool:
836-
"""Return True if the compiler should emit a warning about this name being redefined."""
837-
no_warn_on_redef = def_meta.val_at(SYM_NO_WARN_ON_REDEF_META_KEY, False)
838-
if no_warn_on_redef:
839-
return False
840-
841-
current_ns = ctx.current_ns
842-
if defsym in current_ns.interns:
843-
var = current_ns.find(defsym)
844-
assert var is not None, f"Var {defsym} cannot be none here"
845-
846-
if var.meta is not None and var.meta.val_at(SYM_REDEF_META_KEY):
847-
return False
848-
else:
849-
return bool(var.is_bound)
850-
else:
851-
return safe_name in current_ns.module.__dict__
852-
853-
854829
@_with_ast_loc
855830
def _def_to_py_ast( # pylint: disable=too-many-locals
856831
ctx: GeneratorContext, node: Def
@@ -878,14 +853,6 @@ def _def_to_py_ast( # pylint: disable=too-many-locals
878853
else []
879854
)
880855

881-
# Warn if this symbol is potentially being redefined (if the Var was
882-
# previously bound)
883-
if node.var.is_bound and __should_warn_on_redef(ctx, defsym, safe_name, def_meta):
884-
logger.warning(
885-
f"redefining local Python name '{safe_name}' in module "
886-
f"'{ctx.current_ns.module.__name__}' ({node.env.ns}:{node.env.line})"
887-
)
888-
889856
if node.init is not None:
890857
# Since Python function definitions always take the form `def name(...):`,
891858
# it is redundant to assign them to the their final name after they have

0 commit comments

Comments
 (0)