Skip to content
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

Incorrect span for unknown field error #593

Closed
nissaofthesea opened this issue Jul 31, 2023 · 2 comments
Closed

Incorrect span for unknown field error #593

nissaofthesea opened this issue Jul 31, 2023 · 2 comments
Labels
A-error Area: Error reporting C-bug Category: Things not working as expected

Comments

@nissaofthesea
Copy link

nissaofthesea commented Jul 31, 2023

I have an issue in a larger project where toml sometimes reports incorrect spans for missing fields declared at the top level.
Below is a minimal recreation of what I think is the same issue.

Cargo.toml:

[package]
name = "toml-span-bug"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
serde = { version = "1.0.178", default-features = false, features = ["derive"] }
toml = { version = "0.7.6", default-features = false, features = ["parse"] }

main.rs:

use serde::Deserialize;
use toml;

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
    pub a: i32,
    #[serde(flatten)]
    pub b: B,
}

#[derive(Deserialize)]
pub struct B {}

fn main() {
    match toml::from_str::<Config>(
        r#"a = 1
unknown = 1"#,
    ) {
        Ok(_) => {}
        Err(err) => {
            println!("{err}");
        }
    }
}

Running this gives the following output:

$ cargo run
TOML parse error at line 1, column 1
  |
1 | a = 1
  | ^^^^^
unknown field `unknown`

I would expect the following span instead:

TOML parse error at line 2, column 1
  |
2 | unknown = 1
  | ^^^^^^^^^^^
unknown field `unknown`

Interestingly, the span stays the same if I add a newline at the top of the source, so it's not tied to the field a.

17c17,18
<         r#"a = 1
---
>         r#"
> a = 1
$ cargo run
TOML parse error at line 1, column 1
  |
1 | 
  | ^
unknown field `unknown`
@epage epage added C-bug Category: Things not working as expected A-error Area: Error reporting labels Jul 31, 2023
@nissaofthesea
Copy link
Author

#589 is probably related

@epage
Copy link
Member

epage commented Mar 8, 2024

Closing in favor of #589

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-error Area: Error reporting C-bug Category: Things not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants