[FOLLOW UP] Type check
- Dominant language
- Rust
- Stars
- 1.7k
- Forks
- 218
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 9
Description
* [x] Refine name resolution cache functionality and merge path analysis pass with def analysis, this probably should be done before implementing #988
---
* [x] Integrate method selection into type inference context properly. For example, `s.foo` should be resolved to `Trait::method` without any ambiguity. (Resolved in #1007)
```rust
trait Trait {
fn method(self) -> T
}
struct S {}
impl Trait for S {
fn method(self) -> i32 {
1
}
}
impl Trait for S {
fn method(self) -> u32 {
1
}
}
fn foo() -> u32 {
let s = S {};
s.method()
}
```
This could be achieved by maintaining the mapping between a canonical type and a given receiver type in a call site.
Also, the method selector needs to return a `TraitInst` that relates to the receiver type. In the above example, it will be `Canonical>`. The below one is a more complex case,
```rust
trait Default {
fn default() -> Self
}
trait Foo {
fn foo(self) -> (T, U)
}
struct S {
t: T,
}
impl S {
fn new() -> Self
where
T: Default,
{
Self { t: T::default() }
}
}
impl Foo for S {
fn foo(self) -> (i32, T) {
(1, self.t)
}
}
impl Foo for S {
fn foo(self) -> (u32, T) {
(1, self.t)
}
}
fn bar() -> (u32, i32) {
let s = S::new()
s.foo()
}
```
In this case, assuming the type of `s` is `S` in the inference context and its canonical form is `Canonical>`, then the method selector needs to return `Canonical, ?1, ?0>>` as a candidate for `s.foo`. Then, it should be decanonicalized to `Trait, ?fresh, ?10>` in the inference context, where `?fresh` is a new type variable.
---
* [ ] Improve error message when function call doesn't satisfy constraints.
Currently, the error message is not so good when function argument/return type doesn't satisfy the constraints.
e.g.,
```rust
fn foo(t: Option, u: T) {}
fn bar() {
let opt = Some("FOO")
foo(t: opt, u: "FOO")
}
```
emits the below error currently.
```
error[6-0003]: trait bound is not satisfied
┌─ foo.fe:12:5
│
12 │ foo(t: opt, u: "FOO")
│ ^^^ `String<3>` doesn't implement `Copy`
```
To address this issue generally, we need to maintain how the generic parameter relates to the argument/return type given by the call site in the type inference phase.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in the type inference phase, focusing on function-call handling and how generic parameters are related to call-site argument and return types. Reproduce the shown `Copy` constraint failure and trace the existing diagnostic path. Done means function-call constraint errors explain the failing generic relationship clearly rather than only reporting that a trait bound is unsatisfied.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100