Expression results should not be aliases.

This CL updates the expression class to set the alias value instead of
an alias as the result type.

Change-Id: If19ae394a09ba0dc76380514e53a488bbb5a7292
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20140
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 299e340..e3e4254 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -451,6 +451,7 @@
     "src/ast/decorated_variable_test.cc",
     "src/ast/else_statement_test.cc",
     "src/ast/entry_point_test.cc",
+    "src/ast/expression_test.cc",
     "src/ast/fallthrough_statement_test.cc",
     "src/ast/float_literal_test.cc",
     "src/ast/function_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7c0f913..12bbea8 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -267,6 +267,7 @@
   ast/decorated_variable_test.cc
   ast/else_statement_test.cc
   ast/entry_point_test.cc
+  ast/expression_test.cc
   ast/fallthrough_statement_test.cc
   ast/float_literal_test.cc
   ast/function_test.cc
diff --git a/src/ast/expression.cc b/src/ast/expression.cc
index e12dc27..402b8eb 100644
--- a/src/ast/expression.cc
+++ b/src/ast/expression.cc
@@ -24,6 +24,7 @@
 #include "src/ast/constructor_expression.h"
 #include "src/ast/identifier_expression.h"
 #include "src/ast/member_accessor_expression.h"
+#include "src/ast/type/alias_type.h"
 #include "src/ast/unary_derivative_expression.h"
 #include "src/ast/unary_method_expression.h"
 #include "src/ast/unary_op_expression.h"
@@ -37,6 +38,14 @@
 
 Expression::~Expression() = default;
 
+void Expression::set_result_type(type::Type* type) {
+  // The expression result should never be an alias type
+  while (type->IsAlias()) {
+    type = type->AsAlias()->type();
+  }
+  result_type_ = type;
+}
+
 bool Expression::IsArrayAccessor() const {
   return false;
 }
diff --git a/src/ast/expression.h b/src/ast/expression.h
index da20aa8..1ad81d0 100644
--- a/src/ast/expression.h
+++ b/src/ast/expression.h
@@ -43,7 +43,7 @@
 
   /// Sets the resulting type of this expression
   /// @param type the result type to set
-  void set_result_type(type::Type* type) { result_type_ = type; }
+  void set_result_type(type::Type* type);
   /// @returns the resulting type from this expression
   type::Type* result_type() const { return result_type_; }
 
diff --git a/src/ast/expression_test.cc b/src/ast/expression_test.cc
new file mode 100644
index 0000000..de72167
--- /dev/null
+++ b/src/ast/expression_test.cc
@@ -0,0 +1,57 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/ast/expression.h"
+
+#include "gtest/gtest.h"
+#include "src/ast/type/alias_type.h"
+#include "src/ast/type/i32_type.h"
+
+namespace tint {
+namespace ast {
+namespace {
+
+class Expr : public Expression {
+ public:
+  Expr() : Expression() {}
+
+  bool IsValid() const override { return true; }
+  void to_str(std::ostream&, size_t) const override {}
+};
+
+using ExpressionTest = testing::Test;
+
+TEST_F(ExpressionTest, set_result_type) {
+  type::I32Type i32;
+
+  Expr e;
+  e.set_result_type(&i32);
+  ASSERT_NE(e.result_type(), nullptr);
+  EXPECT_TRUE(e.result_type()->IsI32());
+}
+
+TEST_F(ExpressionTest, set_result_type_alias) {
+  type::I32Type i32;
+  type::AliasType a("a", &i32);
+  type::AliasType b("b", &a);
+
+  Expr e;
+  e.set_result_type(&b);
+  ASSERT_NE(e.result_type(), nullptr);
+  EXPECT_TRUE(e.result_type()->IsI32());
+}
+
+}  // namespace
+}  // namespace ast
+}  // namespace tint
\ No newline at end of file