Skip to content

Multiline highlights in diagnostic get noisy quickly #38246

Closed
@killercup

Description

@killercup

This code (playground)

trait Foo<'a, 'b> {
    type Bar;
}

struct Baz {}

struct Dolor<'c, 'd> {
    sit: &'d &'c str,
}

impl<'a, 'b> Foo<'a, 'b> for Baz where 'a: 'b {
    type Bar = &'a Dolor<
        'a,
        'b
    >;
}

fn main() {}

generates these diagnostic messages:

rustc 1.15.0-nightly (daf8c1dfc 2016-12-05)
error[E0491]: in type `&'d &'c str`, reference has a longer lifetime than the data it references
 --> <anon>:8:5
  |
8 |     sit: &'d &'c str,
  |     ^^^^^^^^^^^^^^^^
  |
note: the pointer is valid for the lifetime 'd as defined on the struct at 7:0
 --> <anon>:7:1
  |
7 |   struct Dolor<'c, 'd> {
  |  _^ starting here...
8 | |     sit: &'d &'c str,
9 | | }
  | |_^ ...ending here
note: but the referenced data is only valid for the lifetime 'c as defined on the struct at 7:0
 --> <anon>:7:1
  |
7 |   struct Dolor<'c, 'd> {
  |  _^ starting here...
8 | |     sit: &'d &'c str,
9 | | }
  | |_^ ...ending here

error[E0491]: in type `&'a Dolor<'a, 'b>`, reference has a longer lifetime than the data it references
  --> <anon>:12:5
   |
12 |       type Bar = &'a Dolor<
   |  _____^ starting here...
13 | |         'a,
14 | |         'b
15 | |     >;
   | |______^ ...ending here
   |
   = note: the pointer is valid for the lifetime 'a as defined on unknown free region bounded by scope CodeExtent(15/DestructionScope(NodeId(33)))
   = note: but the referenced data is only valid for the lifetime 'b as defined on unknown free region bounded by scope CodeExtent(15/DestructionScope(NodeId(33)))

error: aborting due to 2 previous errors

Especially the markers on the struct are making it pretty weird to read/recognize the code. (Colored output makes it a bit easier to differentiate the code symbols from the diagnostics ascii art though! 😄)

There probably are some opportunities to make concrete errors highlight more specific parts of the code. Sadly, this only gets worse for less straightforward code. Here's an example from an error I recently saw while fiddling with a macro in diesel:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
   --> src/macros/insertable.rs:473:13
    |
473 |               type Values = (
    |  _____________^ starting here...
474 | |                 ColumnInsertValue<
475 | |                     posts::tags,
476 | |                     AsExpr<&'insert &'a [&'a str], posts::tags>,
477 | |                 >,
478 | |             );
    | |______________^ ...ending here
    |
    = note: first, the lifetime cannot outlive the lifetime 'insert as defined on unknown free region bounded by scope CodeExtent(19922/DestructionScope(NodeId(398)))...
note: ...so that types are compatible (expected expression::AsExpression<pg::types::sql_types::Array<types::Text>>, found expression::AsExpression<pg::types::sql_types::Array<types::Text>>)

This would require highlighting both 'as, right?

cc @estebank who worked on #37369

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions