# HG changeset patch # User David Anderson # Date 1356768747 28800 # Node ID 82b1f8bc93a9028f5c0081c7afdfb47684f98581 # Parent 372adeda3b22729635a656f318c5e8e99ae24bb9 Infer fixed array types from string literals. diff -r 372adeda3b22 -r 82b1f8bc93a9 src/compiler/NameBinding.cpp --- a/src/compiler/NameBinding.cpp Fri Dec 28 22:47:05 2012 -0800 +++ b/src/compiler/NameBinding.cpp Sat Dec 29 00:12:27 2012 -0800 @@ -572,15 +572,16 @@ // Do some extra inference based on the initializer, if present. Expression *init = node->initialization(); - if (init && - init->isArrayLiteral() && - init->toArrayLiteral()->isFixed()) - { - Local contained(zone_, type->isArray() - ? ArrayType::cast(type)->contained() - : type); - if (!inferFixedArrayDimensions(init->toArrayLiteral(), contained, dims, levels)) - return; + if (init) { + if (init->isArrayLiteral() && init->toArrayLiteral()->isFixed()) { + Local contained(zone_, type->isArray() + ? ArrayType::cast(type)->contained() + : type); + if (!inferFixedArrayDimensions(init->toArrayLiteral(), contained, dims, levels)) + return; + } else if (init->isStringLiteral()) { + dims[levels - 1] = init->toStringLiteral()->string()->length() + 1; + } } // If we got extra dimensions, we need to build a new type. diff -r 372adeda3b22 -r 82b1f8bc93a9 tests/basic/dependent-strings.sp --- a/tests/basic/dependent-strings.sp Fri Dec 28 22:47:05 2012 -0800 +++ b/tests/basic/dependent-strings.sp Sat Dec 29 00:12:27 2012 -0800 @@ -8,7 +8,10 @@ public main() { new char:fixed[100] = "crab\n"; - new char:dynamo[] = "cake\n"; + new char:dynamo[]; + + dynamo = "cake\n"; + f(fixed[1]); f(dynamo[1]); PrintString(fixed[1]);