mirror of
https://github.com/m-labs/artiq.git
synced 2024-12-11 12:46:37 +08:00
Teach closures to LocalAccessValidator.
This commit is contained in:
parent
2c010b10ee
commit
7e3f91c0bb
@ -73,27 +73,38 @@ class LocalAccessValidator:
|
|||||||
# Update the state based on block contents, while validating
|
# Update the state based on block contents, while validating
|
||||||
# that no access to uninitialized variables will be done.
|
# that no access to uninitialized variables will be done.
|
||||||
for insn in block.instructions:
|
for insn in block.instructions:
|
||||||
|
def pred_at_fault(env, var_name):
|
||||||
|
# Find out where the uninitialized state comes from.
|
||||||
|
for pred, pred_state in zip(forward_edge_preds, pred_states):
|
||||||
|
if not pred_state[env][var_name]:
|
||||||
|
return pred
|
||||||
|
|
||||||
|
# It's the entry block and it was never initialized.
|
||||||
|
return None
|
||||||
|
|
||||||
if isinstance(insn, (ir.SetLocal, ir.GetLocal)) and \
|
if isinstance(insn, (ir.SetLocal, ir.GetLocal)) and \
|
||||||
"." not in insn.var_name:
|
"." not in insn.var_name:
|
||||||
env, var_name = insn.environment(), insn.var_name
|
env, var_name = insn.environment(), insn.var_name
|
||||||
assert env in block_state
|
|
||||||
assert var_name in block_state[env]
|
|
||||||
|
|
||||||
if isinstance(insn, ir.SetLocal):
|
# Make sure that the variable is defined in the scope of this function.
|
||||||
# We've just initialized it.
|
if env in block_state and var_name in block_state[env]:
|
||||||
block_state[env][var_name] = True
|
if isinstance(insn, ir.SetLocal):
|
||||||
else: # isinstance(insn, ir.GetLocal)
|
# We've just initialized it.
|
||||||
|
block_state[env][var_name] = True
|
||||||
|
else: # isinstance(insn, ir.GetLocal)
|
||||||
|
if not block_state[env][var_name]:
|
||||||
|
# Oops, accessing it uninitialized.
|
||||||
|
self._uninitialized_access(insn, var_name,
|
||||||
|
pred_at_fault(env, var_name))
|
||||||
|
|
||||||
|
if isinstance(insn, ir.Closure):
|
||||||
|
env = insn.environment()
|
||||||
|
for var_name in block_state[env]:
|
||||||
if not block_state[env][var_name]:
|
if not block_state[env][var_name]:
|
||||||
# Oops, accessing it uninitialized. Find out where
|
# A closure would capture this variable while it is not always
|
||||||
# the uninitialized state comes from.
|
# initialized. Note that this check is transitive.
|
||||||
pred_at_fault = None
|
self._uninitialized_access(insn, var_name,
|
||||||
for pred, pred_state in zip(forward_edge_preds, pred_states):
|
pred_at_fault(env, var_name))
|
||||||
if not pred_state[env][var_name]:
|
|
||||||
pred_at_fault = pred
|
|
||||||
assert pred_at_fault is not None
|
|
||||||
|
|
||||||
# Report the error.
|
|
||||||
self._uninitialized_access(insn, pred_at_fault)
|
|
||||||
|
|
||||||
# Save the state.
|
# Save the state.
|
||||||
state[block] = block_state
|
state[block] = block_state
|
||||||
@ -103,19 +114,35 @@ class LocalAccessValidator:
|
|||||||
for block in func.basic_blocks:
|
for block in func.basic_blocks:
|
||||||
traverse(block)
|
traverse(block)
|
||||||
|
|
||||||
def _uninitialized_access(self, insn, pred_at_fault):
|
def _uninitialized_access(self, insn, var_name, pred_at_fault):
|
||||||
uninitialized_loc = None
|
if pred_at_fault is not None:
|
||||||
for pred_insn in reversed(pred_at_fault.instructions):
|
uninitialized_loc = None
|
||||||
if pred_insn.loc is not None:
|
for pred_insn in reversed(pred_at_fault.instructions):
|
||||||
uninitialized_loc = pred_insn.loc.begin()
|
if pred_insn.loc is not None:
|
||||||
break
|
uninitialized_loc = pred_insn.loc.begin()
|
||||||
assert uninitialized_loc is not None
|
break
|
||||||
|
assert uninitialized_loc is not None
|
||||||
|
|
||||||
|
note = diagnostic.Diagnostic("note",
|
||||||
|
"variable is not initialized when control flows from this point", {},
|
||||||
|
uninitialized_loc)
|
||||||
|
else:
|
||||||
|
note = None
|
||||||
|
|
||||||
|
if note is not None:
|
||||||
|
notes = [note]
|
||||||
|
else:
|
||||||
|
notes = []
|
||||||
|
|
||||||
|
if isinstance(insn, ir.Closure):
|
||||||
|
diag = diagnostic.Diagnostic("error",
|
||||||
|
"variable '{name}' can be captured in a closure uninitialized here",
|
||||||
|
{"name": var_name},
|
||||||
|
insn.loc, notes=notes)
|
||||||
|
else:
|
||||||
|
diag = diagnostic.Diagnostic("error",
|
||||||
|
"variable '{name}' is not always initialized here",
|
||||||
|
{"name": var_name},
|
||||||
|
insn.loc, notes=notes)
|
||||||
|
|
||||||
note = diagnostic.Diagnostic("note",
|
|
||||||
"variable is not initialized when control flows from this point", {},
|
|
||||||
uninitialized_loc)
|
|
||||||
diag = diagnostic.Diagnostic("error",
|
|
||||||
"variable '{name}' is not always initialized at this point",
|
|
||||||
{"name": insn.var_name},
|
|
||||||
insn.loc, notes=[note])
|
|
||||||
self.engine.process(diag)
|
self.engine.process(diag)
|
||||||
|
@ -18,3 +18,10 @@ else:
|
|||||||
t = 1
|
t = 1
|
||||||
# CHECK-L: ${LINE:+1}: error: variable 't' is not always initialized
|
# CHECK-L: ${LINE:+1}: error: variable 't' is not always initialized
|
||||||
-t
|
-t
|
||||||
|
|
||||||
|
# CHECK-L: ${LINE:+1}: error: variable 't' can be captured in a closure uninitialized
|
||||||
|
lambda: t
|
||||||
|
|
||||||
|
# CHECK-L: ${LINE:+1}: error: variable 't' can be captured in a closure uninitialized
|
||||||
|
def f():
|
||||||
|
return t
|
||||||
|
Loading…
Reference in New Issue
Block a user