Skip to content

Invalid span when emitting a diagnostic note on the top-level module #63091

Closed
@jakubadamw

Description

@jakubadamw

While I was investigating a fix for #63064, I noticed one problematic error case, where, as it turns out, the compiler emits a broken span.

fn main() {}

trait Foo<X = Box<dyn Foo>> {}

This is the error I'm getting with the nightly:

error[E0391]: cycle detected when processing `Foo::X`
 --> ./src/test/ui/cycle-trait/cycle-trait-default-type-trait-oneliner.rs:3:23
  |
3 | trait Foo<X = Box<dyn Foo>> {}
  |                       ^^^
  |
  = note: ...which again requires processing `Foo::X`, completing the cycle
note: cycle used when collecting item types in top-level module
 --> ./src/test/ui/cycle-trait/cycle-trait-default-type-trait-oneliner.rs:1:1
  |
1 | fn main() {}
  | ^^^^^^^^^

The second span should extend to the whole top-level module but instead terminates at the first {. This is due to how SourceMap::def_span() is defined:

pub fn def_span(&self, sp: Span) -> Span {
self.span_until_char(sp, '{')
}

This works kind of okay for all item definitions, except for top-level modules. I did try to come up with a way to fix it up in libsyntax, but there's no clean way of doing it as it only deals with Spans. I figured the place to look at would be:

if let Some((span, query)) = usage {
err.span_note(fix_span(span, &query),
&format!("cycle used when {}", query.describe(self)));

but I am not at all familiar with the new query system, so decided to file this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-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