From ecb01d055fec6475e5783ebe92e3c928653e28d4 Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 14 Sep 2016 23:28:55 +0000 Subject: [PATCH] compiler: warn about unused kernel_invariant entries. Fixes #543. --- artiq/compiler/embedding.py | 24 +++++++++++++++++++ .../embedding/warning_invariant_missing.py | 20 ++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 artiq/test/lit/embedding/warning_invariant_missing.py diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index f55d9af7f..d935c6197 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -736,6 +736,30 @@ class Stitcher: # 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("", "") self.typedtree = asttyped.ModuleT( diff --git a/artiq/test/lit/embedding/warning_invariant_missing.py b/artiq/test/lit/embedding/warning_invariant_missing.py new file mode 100644 index 000000000..6fc91028f --- /dev/null +++ b/artiq/test/lit/embedding/warning_invariant_missing.py @@ -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 "" + +i = c() + +@kernel +def entrypoint(): + # CHECK-L: :1: warning: object of type 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