about summary refs log tree commit diff stats
path: root/047check_type_by_name.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-02-15 12:43:51 -0800
committerKartik K. Agaram <vc@akkartik.com>2016-02-15 12:43:51 -0800
commitcf36334305b82dc36f7a2423382ccd8b21bb4eae (patch)
tree280f183da5a2d1d1d7ab97c4fc9c9ad4494cb2c3 /047check_type_by_name.cc
parentc36eb25ca3ac555ba7c27796e0ba84a7de696be4 (diff)
downloadmu-cf36334305b82dc36f7a2423382ccd8b21bb4eae.tar.gz
2659 - disallow dynamic arrays in recipes
The rule is: every 'local' variable in a recipe must have a fixed size.
Arrays can only be directly used in a recipe if their type includes a
size. But we haven't been warning about this, and attempts to use array
variables could cause silent memory corruption.

(Hopefully this is the last hole in our type system.)
Diffstat (limited to '047check_type_by_name.cc')
-rw-r--r--047check_type_by_name.cc21
1 files changed, 20 insertions, 1 deletions
diff --git a/047check_type_by_name.cc b/047check_type_by_name.cc
index 88c406c7..74f61e24 100644
--- a/047check_type_by_name.cc
+++ b/047check_type_by_name.cc
@@ -55,8 +55,20 @@ void check_type(map<string, type_tree*>& type, map<string, string_tree*>& type_n
   }
   if (!contains_key(type_name, x.name))
     put(type_name, x.name, x.properties.at(0).second);
-  if (!types_strictly_match(type[x.name], x.type))
+  if (!types_strictly_match(type[x.name], x.type)) {
     raise_error << maybe(get(Recipe, r).name) << x.name << " used with multiple types\n" << end();
+    return;
+  }
+  if (type_name[x.name]->value == "array") {
+    if (!type_name[x.name]->right) {
+      raise_error << maybe(get(Recipe, r).name) << x.name << " can't be just an array. What is it an array of?\n" << end();
+      return;
+    }
+    if (!type_name[x.name]->right->right) {
+      raise_error << get(Recipe, r).name << " can't determine the size of array variable " << x.name << ". Either allocate it separately and make the type of " << x.name << " address:shared:..., or specify the length of the array in the type of " << x.name << ".\n" << end();
+      return;
+    }
+  }
 }
 
 :(scenario transform_fills_in_missing_types)
@@ -93,3 +105,10 @@ recipe main [
   *y <- copy 67
 ]
 +error: main: unknown type charcter in 'y:address:shared:charcter <- new character:type'
+
+:(scenario array_type_without_size_fails)
+% Hide_errors = true;
+recipe main [
+  x:array:number <- merge 2, 12, 13
+]
++error: main can't determine the size of array variable x. Either allocate it separately and make the type of x address:shared:..., or specify the length of the array in the type of x.