Rust: Highlight only relevant parts of type path in type errors

Created on 7 Jan 2019  路  5Comments  路  Source: rust-lang/rust

On E0308 type mismatch errors we already perform a basic check to type arguments in order to only highlight differences between the expected and found type. I recently came across a case where the two types had identifiers of the same length with no type arguments, but that were not local so the path was quite long. It took me some time to realize that the two types were actually different and not caused by different crate versions. It'd be nice if for the following, only the parts of the path that differ were highlighted/bolded in the CLI output:

expected: foo::bar::Baz
   found: foo::bar::Bar
                    ^^^ should highlight only this area

CC #43354

A-diagnostics C-enhancement D-papercut E-medium P-medium T-compiler

Most helpful comment

Hello, I'd like to implement this feature.

All 5 comments

Hello, I'd like to implement this feature.

Hi @kevgrasso! Will you be needing a hand? To begin with, I would recommend searching for DiagnosticStyledString in order to find how it works today. Particularly, this method shows how the highlighting for specific type arguments is currently handled. It is extremely verbose, but the comments should help somewhat. Also, the rustc guide is a good primer on how to develop in the compiler's codebase. If you need a hand, don't hesitate to reach out.

Thanks, I'd found that struct and file, but I'd overlooked that method due to how complicated it seemed at a glance.

@estebank Should I add a test for this? If so, how would I implement that? The standard UI testing method doesn't seem to track highlights.

@kevgrasso sadly, we do not have any way to test correct application of colors in the test suite. Attach screenshots to the PR so that we can see the changes, but we have to rely on being extra careful when touching this part of the codebase.

Was this page helpful?
0 / 5 - 0 ratings