google / google/xls

Crash when _not_ using an associated function in an impl of a generic struct

Open
#4,901 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

**Describe the bug**
Consider the following code:

```rust
#![feature(generics)]

struct Foo { x: T }

impl Foo {
fn default() -> Self { Foo { x: 0 } }
fn from(value: T) -> Self { Foo { x: value } }
fn add(self, other: Foo) -> Self {
Foo { x: self.x + other.x }
}
}

#[test]
fn foo_test() {
let a = Foo::from(40);
let b = Foo::from(2);
let result = a.add(b);
assert_eq(result.x, 42);
}
```

**To Reproduce**
Running that with the interpreter and enabling comparing with jit results in a crash:

```
$ bazel-bin/xls/dslx/interpreter_main --compare=jit jit-break.x
F0903 11:07:34.902128 237246 function_converter.cc:210] Check failed: requires_opt.has_value()
[symbolize_elf.inc : 356] RAW: Unable to get high fd: rc=0, limit=1024
*** Check failure stack trace: ***
@ 0x55555a506e74 absl::lts_20260526::log_internal::LogMessage::SendToLog()
@ 0x55555a506e22 absl::lts_20260526::log_internal::LogMessage::Flush()
@ 0x555556e69f3f xls::dslx, ::GetRequiresImplicitToken()
@ 0x555556e99b18 xls::dslx::FunctionConverter::HandleFunction()
@ 0x555556e526eb xls::dslx::(anonymous namespace)::ConvertCallGraph()
@ 0x555556e5041e xls::dslx::ConvertModuleIntoPackage()
@ 0x555556e54070 xls::dslx::ConvertModuleToPackage()
@ 0x555556e386a4 xls::dslx::AbstractTestRunner::ParseAndTest()
@ 0x555556e0b4d0 main
@ 0x7ffff762b285 __libc_start_call_main
@ 0x7ffff762b338 __libc_start_main@GLIBC_2.2.5
@ 0x555556e0936a _start
```

Commenting out the `fn defautl()` will make this problem go away. So the crash is due to _not_ ever calling deafault()...

**Expected behavior**
No crash :)

**Additional debug steps**

Adding a bit more debug information where that happens:

```patch
--- a/xls/dslx/ir_convert/function_converter.cc
+++ b/xls/dslx/ir_convert/function_converter.cc
@@ -207,7 +207,7 @@ bool GetRequiresImplicitToken(const dslx::Function& f, ImportData* import_data,
std::optional requires_opt =
import_data->GetRootTypeInfo().value()->GetRequiresImplicitToken(f);

- CHECK(requires_opt.has_value());
+ CHECK(requires_opt.has_value()) << f.ToString();
bool requires_for_test = options.convert_tests && f.parent() != nullptr &&
f.parent()->kind() == AstNodeKind::kTestFunction;
return requires_opt.value() || requires_for_test;
```

Shows that it runs into a problem with the default() function
```
F0903 11:10:59.412232 238506 function_converter.cc:210] Check failed: requires_opt.has_value() fn default() -> Self {
Foo { x: 0 }
}
```

**Environment (this can be helpful for troubleshooting):**
XLS compiled from head.

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with xls/dslx/interpreter_main --compare=jit on the generic-struct example. Start in xls/dslx/ir_convert/function_converter.cc at GetRequiresImplicitToken and follow its call from FunctionConverter::HandleFunction; use the supplied diagnostic to investigate why the uncalled default() function lacks the required information. Done means the example completes without crashing when interpreter and JIT results are compared.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.