@@ -4235,6 +4235,56 @@ fn test_f() {
42354235 XLS_EXPECT_OK (Typecheck (kProgram ));
42364236}
42374237
4238+ TEST (TypecheckTest, MatchPackageLevelConstant) {
4239+ constexpr std::string_view kProgram = R"(
4240+ const FOO = u8:0xff;
4241+ fn f(x: u8) -> u2 {
4242+ match x {
4243+ FOO => u2:0,
4244+ _ => u2:0,
4245+ }
4246+ }
4247+ )" ;
4248+ XLS_ASSERT_OK_AND_ASSIGN (TypecheckResult result, Typecheck (kProgram ));
4249+ const TypeInfo& type_info = *result.tm .type_info ;
4250+ // Get the pattern match for the first arm of the match expression inside of
4251+ // function `f`.
4252+ Function* f = result.tm .module ->GetFunction (" f" ).value ();
4253+ Statement* stmt = f->body ()->statements ()[0 ];
4254+ Expr* expr = std::get<Expr*>(stmt->wrapped ());
4255+ Match* m = dynamic_cast <Match*>(expr);
4256+ ASSERT_NE (m, nullptr );
4257+ const MatchArm* arm = m->arms ()[0 ];
4258+ const NameDefTree* pattern = arm->patterns ()[0 ];
4259+
4260+ // Check that the pattern is just a leaf NameRef.
4261+ NameRef* name_ref = std::get<NameRef*>(pattern->leaf ());
4262+ ASSERT_NE (name_ref, nullptr );
4263+ EXPECT_EQ (name_ref->identifier (), " FOO" );
4264+
4265+ std::optional<InterpValue> const_expr =
4266+ type_info.GetConstExprOption (name_ref);
4267+ ASSERT_TRUE (const_expr.has_value ());
4268+ EXPECT_EQ (const_expr->ToString (), " u8:255" );
4269+ }
4270+
4271+ TEST (TypecheckTest, MatchPackageLevelConstantIntoTypeAlias) {
4272+ constexpr std::string_view kProgram = R"(
4273+ const C = u32:2;
4274+ fn f(x: u32) -> u2 {
4275+ match x {
4276+ C => {
4277+ const D = C;
4278+ type T = uN[D];
4279+ T::MAX
4280+ },
4281+ _ => u2:0,
4282+ }
4283+ }
4284+ )" ;
4285+ XLS_EXPECT_OK (Typecheck (kProgram ));
4286+ }
4287+
42384288// Table-oriented test that lets us validate that *types on parameters* are
42394289// compatible with *particular values* that should be type-compatible.
42404290TEST (PassValueToIdentityFnTest, ParameterVsValue) {
0 commit comments