Skip to content

Commit

Permalink
Merge pull request #1699 from xlsynth:cdleary/2024-11-06-unhandled-fo…
Browse files Browse the repository at this point in the history
…r-deduction

PiperOrigin-RevId: 694712466
  • Loading branch information
copybara-github committed Nov 9, 2024
2 parents 4d1423f + ab678d3 commit 210b8ab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
14 changes: 12 additions & 2 deletions xls/dslx/type_system/deduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2079,14 +2079,24 @@ class DeduceVisitor : public AstNodeVisitor {
return Fatal(n);
}
absl::Status HandleFunctionRef(const FunctionRef* n) override {
return Fatal(n);
XLS_ASSIGN_OR_RETURN(std::unique_ptr<Type> callee_type,
ctx_->Deduce(n->callee()));
if (!callee_type->IsFunction()) {
return TypeInferenceErrorStatus(
n->span(), callee_type.get(),
"Callee for function reference must be function-typed",
ctx_->file_table());
}
result_ = std::move(callee_type);
return absl::OkStatus();
}

absl::StatusOr<std::unique_ptr<Type>>& result() { return result_; }

private:
absl::Status Fatal(const AstNode* n) {
LOG(FATAL) << "Got unhandled AST node for deduction: " << n->ToString();
LOG(FATAL) << "DeduceVisitor got unhandled AST node for deduction: "
<< n->ToString() << " node type name: " << n->GetNodeTypeName();
}

DeduceCtx* ctx_;
Expand Down
4 changes: 4 additions & 0 deletions xls/dslx/type_system/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ bool Type::IsTuple() const {
return dynamic_cast<const TupleType*>(this) != nullptr;
}

bool Type::IsFunction() const {
return dynamic_cast<const FunctionType*>(this) != nullptr;
}

const EnumType& Type::AsEnum() const {
auto* s = dynamic_cast<const EnumType*>(this);
CHECK(s != nullptr) << "Type is not an enum: " << *this;
Expand Down
1 change: 1 addition & 0 deletions xls/dslx/type_system/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ class Type {
bool IsArray() const;
bool IsMeta() const;
bool IsTuple() const;
bool IsFunction() const;

const StructType& AsStruct() const;
const ProcType& AsProc() const;
Expand Down
8 changes: 8 additions & 0 deletions xls/dslx/type_system/typecheck_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,14 @@ proc t {
)"));
}

TEST(TypecheckErrorTest, InstantiateALiteralNumber) {
constexpr std::string_view kProgram = "fn f(x:u2) { 0<> }";
EXPECT_THAT(Typecheck(kProgram).status(),
IsPosError("TypeInferenceError",
HasSubstr("Could not infer a type for this number, "
"please annotate a type.")));
}

TEST(TypecheckErrorTest, SignedValueToBuiltinExpectingUNViaParametric) {
EXPECT_THAT(Typecheck(R"(
fn p<S: bool, N: u32>() -> u32 {
Expand Down

0 comments on commit 210b8ab

Please sign in to comment.