Skip to content

Help message in E0369 points to wrong file #113844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ElectrifyPro opened this issue Jul 19, 2023 · 2 comments · Fixed by #113871
Closed

Help message in E0369 points to wrong file #113844

ElectrifyPro opened this issue Jul 19, 2023 · 2 comments · Fixed by #113871
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.

Comments

@ElectrifyPro
Copy link
Contributor

ElectrifyPro commented Jul 19, 2023

I have the following code, split into two files:

// src/main.rs
// this appears in the error

mod name;

use name::Name;

fn main() {
    let name_1 = Name::from("John Cena");
    let name_2 = Name::from("Not John Cena");
    println!("{}", name_1 == name_2);
}
// src/name.rs
pub struct Name {
    pub first: String,
    pub middle: String,
    pub last: String,
}

impl From<&str> for Name {
    fn from(name: &str) -> Self {
        let mut parts = name.split_whitespace();
        let first = parts.next().unwrap_or_default();
        let last = parts.clone().last().unwrap_or_default();
        let middle = parts.collect::<Vec<_>>().join(" ");
        Self {
            first: first.to_owned(),
            middle,
            last: last.to_owned(),
        }
    }
}

This code, as expected, produces E0369, shown below. However, the help message at the bottom of the error appears to be referencing line 2 in src/main.rs, while displaying the filename src/name.rs, thus suggesting we add the derive annotation to a comment in src/main.rs. It should instead point to the struct in src/name.rs.

error[E0369]: binary operation `==` cannot be applied to type `Name`
  --> src/main.rs:11:27
   |
11 |     println!("{}", name_1 == name_2);
   |                    ------ ^^ ------ Name
   |                    |
   |                    Name
   |
note: an implementation of `PartialEq` might be missing for `Name`
  --> src/name.rs:2:1
   |
2  | pub struct Name {
   | ^^^^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `Name` with `#[derive(PartialEq)]`
  --> src/name.rs:2:1
   |
2  + #[derive(PartialEq)]
3  | // this appears in the error
   |

Meta

rustc --version --verbose:

rustc 1.73.0-nightly (903e279f4 2023-07-18)
binary: rustc
commit-hash: 903e279f468590fa3425f8aff7f3d61a5a873dbb
commit-date: 2023-07-18
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
@ElectrifyPro ElectrifyPro added the C-bug Category: This is a bug. label Jul 19, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 19, 2023
@clubby789
Copy link
Contributor

@rustbot claim

@clubby789 clubby789 added A-diagnostics Area: Messages for errors, warnings, and lints and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 19, 2023
@clubby789
Copy link
Contributor

The issue is that in

let file_lines = sm
.span_to_lines(span.primary_span().unwrap().shrink_to_hi())
.expect("span_to_lines failed when emitting suggestion");
let line_num = sm.lookup_char_pos(parts[0].span.lo()).line;
, the span we use to look up the file/line mapper is the primary span of the message, i.e. the error in main.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants