Skip to content

Commit

Permalink
compiler: warn about unused kernel_invariant entries.
Browse files Browse the repository at this point in the history
Fixes #543.
whitequark committed Sep 14, 2016
1 parent 494cfca commit feeb089
Showing 2 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions artiq/compiler/embedding.py
Original file line number Diff line number Diff line change
@@ -736,6 +736,30 @@ def finalize(self):
# do one last pass unconditionally.
inferencer.visit(self.typedtree)

# After we've discovered every referenced attribute, check if any kernel_invariant
# specifications refers to ones we didn't encounter.
for host_type in self.embedding_map.type_map:
instance_type, constructor_type = self.embedding_map.type_map[host_type]
for attribute in instance_type.constant_attributes:
if attribute in instance_type.attributes:
# Fast path; if the ARTIQ Python type has the attribute, then every observed
# value is guaranteed to have it too.
continue

for value, loc in self.value_map[instance_type]:
if hasattr(value, attribute):
continue

diag = diagnostic.Diagnostic("warning",
"object {value} of type {typ} declares attribute '{attr}' as "
"kernel invariant, but the instance referenced here does not "
"have this attribute",
{"value": repr(value),
"typ": types.TypePrinter().name(instance_type, max_depth=0),
"attr": attribute},
loc)
self.engine.process(diag)

# After we have found all functions, synthesize a module to hold them.
source_buffer = source.Buffer("", "<synthesized>")
self.typedtree = asttyped.ModuleT(
20 changes: 20 additions & 0 deletions artiq/test/lit/embedding/warning_invariant_missing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# RUN: %python -m artiq.compiler.testbench.embedding +diag %s 2>%t
# RUN: OutputCheck %s --file-to-check=%t

from artiq.language.core import *
from artiq.language.types import *

class c:
kernel_invariants = {"a", "b"}
a = 0

def __repr__(self):
return "<testbench.c object>"

i = c()

@kernel
def entrypoint():
# CHECK-L: <synthesized>:1: warning: object <testbench.c object> of type <instance testbench.c> declares attribute 'b' as kernel invariant, but the instance referenced here does not have this attribute
# CHECK-L: ${LINE:+1}: note: expanded from here
i

0 comments on commit feeb089

Please sign in to comment.