Skip to content

Highlight and simplify mismatched types - #41205

Merged
bors merged 1 commit into
rust-lang:masterfrom
estebank:shorter-mismatched-types-2
Apr 12, 2017
Merged

Highlight and simplify mismatched types#41205
bors merged 1 commit into
rust-lang:masterfrom
estebank:shorter-mismatched-types-2

Conversation

@estebank

@estebankestebank commented Apr 10, 2017

Copy link
Copy Markdown
Contributor

Shorten mismatched types errors by replacing subtypes that are not
different with _, and highlighting only the subtypes that are
different.

Given a file

structX<T1,T2>{x:T1,y:T2,}fnfoo() -> X<X<String,String>,String>{X{x:X{x:"".to_string(),y:2},y:"".to_string()}}fnbar() -> Option<String>{"".to_string()}

provide the following output

error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X{x:X{x:"".to_string(),y:2},y:"".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_,{integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`

Fix#21025. Re: #40186. Follow up to #39906.

I'm looking to change how this output is accomplished so that it doesn't create list of strings to pass around, but rather add an elided Ty placeholder, and use the same string formatting for normal types. I'll be doing that soonish.

r? @nikomatsakis

@estebank

Copy link
Copy Markdown
ContributorAuthor

Visual output:

@estebank
estebankforce-pushed the shorter-mismatched-types-2 branch from 8396d0c to a39b3ceCompareApril 11, 2017 10:09
@arielb1

Copy link
Copy Markdown
Contributor

@estebank
Shouldn't the "found type std::string::String" be highlighted as well?

@estebank

Copy link
Copy Markdown
ContributorAuthor

It only highlights differing parts, while dimming the parts that are the same. In that case, only the std::option::Option<...> part is relevant to the error. May be we can incorporate the elision logic without any highlighting change for the "composition" case?

@arielb1

Copy link
Copy Markdown
Contributor

@estebank

I mean, I expected:


note: expected type `std::option::Option<std::string::String>`
^^^^^^^^^^^^^^^^^^^^ ^
found type `std::string::String`
^^^^^^^^^^^^^^^^^^^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax error: mismatched parentheses

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax error: mismatched parentheses

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment threadsrc/librustc_errors/diagnostic.rs Outdated

@arielb1arielb1Apr 11, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this StyledString or something? There's already snippet::StyledString, so maybe have a Vec of that?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@nikomatsakisnikomatsakis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me modulo test failures; @arielb1's thoughts on underlining seem potentially fine, but I could go either way (and even if we did want to do it, I'd be fine w/ leaving it for a later PR).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike _, this is not legal syntax for a user to type, but I guess that's ok. Not sure what else to use, in any case.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your reasoning. I followed this path given that the compiler already has diagnostics that show '_ in some cases.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too bad we lose the highlighting here, but I don't really know how to fix that

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... I've been thinking that we could expand ui errors to have a mode where ANSI escape codes are kept (.stderr_full? A comment flag in the .rs test file?).

@nikomatsakis

Copy link
Copy Markdown
Contributor

I see @arielb1 also posted some other suggestions that seem potentially good; e.g. using StyledString or what have you. I don't have a strong opinion. This didn't seem like a lot of code and it read fairly easily (thanks @estebank for the comments, the code seemed pretty easy to understand this time) but DRYer is always better.

Comment threadsrc/librustc/infer/error_reporting/mod.rs Outdated
@estebank
estebankforce-pushed the shorter-mismatched-types-2 branch 3 times, most recently from a1a2ff5 to f875e08CompareApril 11, 2017 22:44
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
@estebank
estebankforce-pushed the shorter-mismatched-types-2 branch from f875e08 to 2389830CompareApril 11, 2017 22:45
@estebank

Copy link
Copy Markdown
ContributorAuthor

@nikomatsakis I believe I addressed all the points (except the highlighting case brought up by @arielb1), as well as fixed a small presentational bug that was left from when I was first preparing this code.

@estebank

Copy link
Copy Markdown
ContributorAuthor

@bors r=nikomatsakis

@bors

bors commented Apr 12, 2017

Copy link
Copy Markdown
Collaborator

📌 Commit 2389830 has been approved by nikomatsakis

TimNN added a commit to TimNN/rust that referenced this pull request Apr 12, 2017
…, r=nikomatsakis
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
Fixrust-lang#21025. Re: rust-lang#40186. Follow up to rust-lang#39906.
I'm looking to change how this output is accomplished so that it doesn't create list of strings to pass around, but rather add an elided `Ty` placeholder, and use the same string formatting for normal types. I'll be doing that soonish.
r? @nikomatsakis
@TimNNTimNN mentioned this pull request Apr 12, 2017
bors added a commit that referenced this pull request Apr 12, 2017
Rollup of 9 pull requests
- Successful merges: #41063, #41087, #41141, #41166, #41183, #41205, #41206, #41232, #41243
- Failed merges:
@bors
bors merged commit 2389830 into rust-lang:masterApr 12, 2017
@estebank
estebank deleted the shorter-mismatched-types-2 branch November 9, 2023 05:26
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@estebank@arielb1@nikomatsakis@bors