Skip to content

Commit

Permalink
[DSLX:frontend] Don't stop parsing after const_assert! at module sc…
Browse files Browse the repository at this point in the history
…ope.

These were only used in unit tests, but autoformatter test failing was showing
that the AST was prematurely truncated, this fixes it.

PiperOrigin-RevId: 581558662
  • Loading branch information
cdleary authored and copybara-github committed Nov 11, 2023
1 parent 88d1f5a commit cdbf614
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 2 additions & 0 deletions xls/dslx/frontend/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ std::optional<ModuleMember*> Module::FindMemberWithName(
if (std::get<Import*>(member)->identifier() == target) {
return &member;
}
} else if (std::holds_alternative<ConstAssert*>(member)) {
continue; // These have no name / binding.
} else {
XLS_LOG(FATAL) << "Unhandled module member variant: "
<< ToAstNode(member)->GetNodeTypeName();
Expand Down
9 changes: 8 additions & 1 deletion xls/dslx/frontend/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "xls/dslx/frontend/bindings.h"
#include "xls/dslx/frontend/builtins_metadata.h"
#include "xls/dslx/frontend/pos.h"
#include "xls/dslx/frontend/scanner_keywords.inc"
#include "xls/dslx/frontend/token.h"
#include "xls/ir/code_template.h"
#include "xls/ir/foreign_function.h"
Expand Down Expand Up @@ -248,6 +249,8 @@ absl::StatusOr<std::unique_ptr<Module>> Parser::ParseModule(
while (!AtEof()) {
XLS_ASSIGN_OR_RETURN(bool peek_is_eof, PeekTokenIs(TokenKind::kEof));
if (peek_is_eof) {
XLS_VLOG(3) << "Parser saw EOF token for module " << module_->name()
<< ", stopping.";
break;
}

Expand Down Expand Up @@ -341,7 +344,7 @@ absl::StatusOr<std::unique_ptr<Module>> Parser::ParseModule(
// the error lambda.
XLS_RETURN_IF_ERROR(
module_->AddTop(const_assert, /*make_collision_error=*/nullptr));
break;
continue;
}

auto top_level_error = [peek] {
Expand Down Expand Up @@ -409,6 +412,10 @@ absl::StatusOr<std::unique_ptr<Module>> Parser::ParseModule(
}
}

// Ensure we've consumed all tokens when we're done parsing, as a
// post-condition.
XLS_RET_CHECK(AtEof());

XLS_RETURN_IF_ERROR(VerifyParentage(module_.get()));
auto result = std::move(module_);
module_ = nullptr;
Expand Down
2 changes: 0 additions & 2 deletions xls/dslx/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ dslx_lang_test(
name = "module_level_const_assert_that_calls_fn",
# No meaningful entry point to convert.
convert_to_ir = False,
# TODO(leary): 2023-10-20 Deleting contents after first constexpr?!
test_autofmt = False,
)

dslx_lang_test(
Expand Down
8 changes: 2 additions & 6 deletions xls/dslx/tests/module_level_const_assert_that_calls_fn.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

fn f(x: u32) -> u32 {
x + u32:1
}
fn f(x: u32) -> u32 { x + u32:1 }

// Call a (non-parametric) fn.
const_assert!(f(u32:42) == u32:43);

fn add_one<P: u32>() -> u32 {
P + u32:1
}
fn add_one<P: u32>() -> u32 { P + u32:1 }

// Call a parametric fn.
const_assert!(add_one<u32:42>() == u32:43);
Expand Down

0 comments on commit cdbf614

Please sign in to comment.