Skip to content

Commit

Permalink
Merge pull request google#1823 from xlsynth:cdleary/2025-01-01-type-i…
Browse files Browse the repository at this point in the history
…nference-to-feature

PiperOrigin-RevId: 711523076
  • Loading branch information
copybara-github committed Jan 2, 2025
2 parents 5c1f2d7 + 6fc21b8 commit fb07a91
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 85 deletions.
2 changes: 1 addition & 1 deletion xls/dslx/fmt/ast_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2894,7 +2894,7 @@ absl::StatusOr<DocRef> Formatter::Format(const Module& n) {
pieces.push_back(arena_.hard_line());
break;
case ModuleAnnotation::kTypeInferenceVersion2:
pieces.push_back(arena_.MakeText("#![type_inference_version = 2]"));
pieces.push_back(arena_.MakeText("#![feature(type_inference_v2)]"));
pieces.push_back(arena_.hard_line());
break;
case ModuleAnnotation::kAllowUseSyntax:
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/fmt/ast_fmt_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,7 @@ fn id(x: u32) { x }
}

TEST_F(ModuleFmtTest, TypeInferenceVersionAnnotation) {
DoFmt(R"(#![type_inference_version = 2]
DoFmt(R"(#![feature(type_inference_v2)]
fn id(x: u32) { x }
)");
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/frontend/ast_cloner_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ TEST(AstClonerTest, CloneModuleClonesVerbatimNode) {
TEST(AstClonerTest, ModuleLevelAnnotations) {
constexpr std::string_view kProgram =
R"(#![allow(nonstandard_constant_naming)]
#![type_inference_version = 2]
#![feature(type_inference_v2)]
const name = u32:42;)";

Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/frontend/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ std::string Module::ToString() const {
absl::StrAppend(out, "#![allow(nonstandard_member_naming)]");
break;
case ModuleAnnotation::kTypeInferenceVersion2:
absl::StrAppend(out, "#![type_inference_version = 2]");
absl::StrAppend(out, "#![feature(type_inference_v2)]");
break;
case ModuleAnnotation::kAllowUseSyntax:
absl::StrAppend(out, "#![feature(use_syntax)]");
Expand Down
51 changes: 8 additions & 43 deletions xls/dslx/frontend/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,6 @@ std::string ExprRestrictionsToString(ExprRestrictions restrictions) {
return "{no-struct-literal}";
}

absl::StatusOr<std::optional<ModuleAnnotation>>
GetModuleAnnotationForTypeInferenceVersion(const Span& span,
const FileTable& file_table,
int64_t version) {
switch (version) {
case 1:
return std::nullopt;
case 2:
return ModuleAnnotation::kTypeInferenceVersion2;
default:
return ParseErrorStatus(span, "Type inference version must be 1 or 2.",
file_table);
}
}

} // namespace

absl::StatusOr<BuiltinType> Parser::TokenToBuiltinType(const Token& tok) {
Expand Down Expand Up @@ -260,17 +245,17 @@ absl::Status Parser::ParseModuleAttribute() {
if (identifier == "feature") {
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kOParen));
XLS_ASSIGN_OR_RETURN(std::string feature, PopIdentifierOrError());
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCParen));
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCBrack));
if (feature == "use_syntax") {
module_->AddAnnotation(ModuleAnnotation::kAllowUseSyntax);
} else if (feature == "type_inference_v2") {
module_->AddAnnotation(ModuleAnnotation::kTypeInferenceVersion2);
} else {
return ParseErrorStatus(
identifier_span,
absl::StrFormat("Unsupported feature: `%s`", feature));
}
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCParen));
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCBrack));
return absl::OkStatus();
}
if (identifier == "type_inference_version") {
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kEquals));
XLS_RETURN_IF_ERROR(ParseTypeInferenceVersionAttribute());
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCBrack));
return absl::OkStatus();
}
if (identifier != "allow") {
Expand All @@ -291,26 +276,6 @@ absl::Status Parser::ParseModuleAttribute() {
return absl::OkStatus();
}

absl::Status Parser::ParseTypeInferenceVersionAttribute() {
XLS_ASSIGN_OR_RETURN(const Token* inference_version, PeekToken());
if (inference_version->kind() != TokenKind::kNumber) {
return ParseErrorStatus(
inference_version->span(),
"Type inference version must be an unquoted integer.");
}
XLS_RETURN_IF_ERROR(DropToken());
XLS_ASSIGN_OR_RETURN(int64_t inference_version_value,
inference_version->GetValueAsInt64());
XLS_ASSIGN_OR_RETURN(
std::optional<ModuleAnnotation> annotation,
GetModuleAnnotationForTypeInferenceVersion(
inference_version->span(), file_table(), inference_version_value));
if (annotation.has_value()) {
module_->AddAnnotation(*annotation);
}
return absl::OkStatus();
}

absl::StatusOr<std::unique_ptr<Module>> Parser::ParseModule(
Bindings* bindings) {
const Pos module_start_pos = GetPos();
Expand Down
6 changes: 0 additions & 6 deletions xls/dslx/frontend/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,12 +586,6 @@ class Parser : public TokenParser {
// Side-effect: module_ is tagged with the parsed attribute on success.
absl::Status ParseModuleAttribute();

// Parses the value of the `type_inference_version` module level attribute,
// i.e. just the `foo` token in `#![type_inference_version = foo]`, and adds
// the appropriate attribute to `module_` if the default version is not
// indicated.
absl::Status ParseTypeInferenceVersionAttribute();

// Parses DSLX attributes, analogous to Rust's attributes.
//
// This accepts the following attributes:
Expand Down
34 changes: 3 additions & 31 deletions xls/dslx/frontend/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2962,49 +2962,21 @@ TEST_F(ParserTest, ParseAllowNonstandardConstantNamingAnnotation) {
testing::ElementsAre(ModuleAnnotation::kAllowNonstandardConstantNaming));
}

TEST_F(ParserTest, ParseTypeInferenceVersionAttributeWithValue1) {
TEST_F(ParserTest, NoAttributeForTypeInferenceVersion) {
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> module, Parse(R"(
#![type_inference_version = 1]
fn f() { () }
)"));
EXPECT_THAT(module->annotations(), testing::IsEmpty());
}

TEST_F(ParserTest, ParseTypeInferenceVersionAttributeWithValue2) {
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> module, Parse(R"(
#![type_inference_version = 2]
#![feature(type_inference_v2)]
)"));
EXPECT_THAT(module->annotations(),
testing::ElementsAre(ModuleAnnotation::kTypeInferenceVersion2));
}

TEST_F(ParserTest, ParseTypeInferenceVersionAttributeWithValue3Fails) {
absl::StatusOr<std::unique_ptr<Module>> module = Parse(R"(
#![type_inference_version = 3]
)");
EXPECT_THAT(module,
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Type inference version must be 1 or 2.")));
}

TEST_F(ParserTest, ParseTypeInferenceVersionAttributeWithNonIntegerFails) {
absl::StatusOr<std::unique_ptr<Module>> module = Parse(R"(
#![type_inference_version = "2"]
)");
EXPECT_THAT(
module,
StatusIs(
absl::StatusCode::kInvalidArgument,
HasSubstr("Type inference version must be an unquoted integer.")));
}

TEST_F(ParserTest, ParseTypeInferenceVersionAttributeWithParenFormatFails) {
absl::StatusOr<std::unique_ptr<Module>> module = Parse(R"(
#![type_inference_version(2)]
)");
EXPECT_THAT(module, StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Expected '=', got '('")));
}

// Verifies that we can walk backwards through a tree. In this case, from the
// terminal node to the defining expr.
TEST(ParserBackrefTest, CanFindDefiner) {
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/type_system_v2/typecheck_module_v2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ using ::testing::ContainsRegex;
using ::testing::HasSubstr;

absl::StatusOr<TypecheckResult> TypecheckV2(std::string_view program) {
return Typecheck(absl::StrCat("#![type_inference_version = 2]\n\n", program));
return Typecheck(absl::StrCat("#![feature(type_inference_v2)]\n\n", program));
}

// Verifies the type produced by `TypecheckV2`, for the topmost node only, in a
Expand Down

0 comments on commit fb07a91

Please sign in to comment.