Skip to content
Browse files

Merge remote branch 'upstream/master'

  • Loading branch information...
2 parents 8434c31 + fbdd38b commit 819fb12698b9476b31bc1e122eb6afc2a1da8ca6 @felipensp felipensp committed Mar 29, 2012
Showing with 40 additions and 0 deletions.
  1. +14 −0 compiler/typechecker.cc
  2. +13 −0 tests/constness_005.test
  3. +13 −0 tests/constness_006.test
View
14 compiler/typechecker.cc
@@ -668,6 +668,7 @@ AST_VISITOR(TypeChecker, VariableDecl) {
// `auto' typed, so the variable's type is the type of rhs
type = initval->getTypePtr();
var->setTypePtr(type);
+ is_auto = true;
}
// Building ValueVector of arguments for __assign__ method call
@@ -699,6 +700,19 @@ AST_VISITOR(TypeChecker, VariableDecl) {
var->setConstness(expr->isConst());
+ if (rhs) {
+ if (!is_auto && !expr->isConst() && rhs->getValue()->isConst()) {
+ Compiler::errorf(expr->getLocation(),
+ "Cannot convert const-qualified variable `%S' to a "
+ "non-const variable of type `%S' on assignment",
+ variable->getName(),
+ type->getName());
+ }
+ else {
+ var->setConstness(expr->isConst() || rhs->getValue()->isConst());
+ }
+ }
+
// Registers a new variable
m_scope->pushValue(var->getName(), var);
}
View
13 tests/constness_005.test
@@ -0,0 +1,13 @@
+[FATAL] Testing constness #4
+==CODE==
+import std.io.*;
+
+const Auto a = {1, 3, 5};
+println(a);
+
+Auto b = a;
+b.push(-1);
+
+println(b, a);
+==RESULT==
+Compile error: Can't call the non-const method Array<Int>::push\(Int\) because variable `b' is const on \S+ line 7
View
13 tests/constness_006.test
@@ -0,0 +1,13 @@
+[FATAL] Testing constness #4
+==CODE==
+import std.io.*;
+
+Auto a = {1, 3, 5};
+println(a);
+
+const Auto b = a;
+b.push(-1);
+
+println(b, a);
+==RESULT==
+Compile error: Can't call the non-const method Array<Int>::push\(Int\) because variable `b' is const on \S+ line 7

0 comments on commit 819fb12

Please sign in to comment.
Something went wrong with that request. Please try again.