Skip to content

Assert Spans are well-formed #122418

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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_span/src/span_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ impl Span {
ctxt: SyntaxContext,
parent: Option<LocalDefId>,
) -> Self {
if cfg!(debug_assertions) {
assert!(hi > lo, "low end of span is after high");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler would be just debug_assert!. Also, this should print the lo and hi values, so something like:

debug_assert!(hi > lo, "low end of span is after high: lo={lo:?}, hi={hi:?}");

I agree with @petrochenkov that this might not actually be practical, despite the existence of #47504. And the test failure is further evidence of that. The relevant part of the failing stacktrace:

    11:     0x7f292995abb5 - core[301f0576711e1b37]::panicking::panic_fmt
    12:     0x7f292d6fa2a2 - <rustc_parse[cd33da97dab93ae0]::lexer::StringReader>::next_token
    12:     0x7f292d6fa2a2 - <rustc_parse[cd33da97dab93ae0]::lexer::StringReader>::next_token
    13:     0x7f292d6af112 - <rustc_parse[cd33da97dab93ae0]::lexer::tokentrees::TokenTreesReader>::parse_token_trees
    14:     0x7f292d6ae4b8 - <rustc_parse[cd33da97dab93ae0]::lexer::tokentrees::TokenTreesReader>::parse_all_token_trees
    15:     0x7f292d6fe2e2 - rustc_parse[cd33da97dab93ae0]::maybe_file_to_stream
    16:     0x7f292d6fde87 - rustc_parse[cd33da97dab93ae0]::maybe_source_file_to_parser
    17:     0x7f292d6fd9bf - rustc_parse[cd33da97dab93ae0]::maybe_new_parser_from_source_str
    18:     0x7f292a91decb - rustc_interface[a0975ad2d4cbfd75]::interface::parse_check_cfg

(Why is line 12 repeated? Not sure.)

parse_check_cfg is very early in the compiler's execution. Have you run any tests locally with this patch applied, e.g. with "x.py tests tests/ui"? That runs a good subset of the test suite.

}

if lo > hi {
std::mem::swap(&mut lo, &mut hi);
}
Expand Down