Skip to content

SessionDiagnostic: Fix non-determinism in generated format string. #76515

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

Merged
Merged
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
11 changes: 6 additions & 5 deletions compiler/rustc_macros/src/session_diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#![deny(unused_must_use)]
use quote::format_ident;
use quote::quote;

use proc_macro::Diagnostic;
use quote::{format_ident, quote};
use syn::spanned::Spanned;

use std::collections::{HashMap, HashSet};
use std::collections::{BTreeSet, HashMap};

/// Implements #[derive(SessionDiagnostic)], which allows for errors to be specified as a struct, independent
/// from the actual diagnostics emitting code.
Expand Down Expand Up @@ -577,7 +575,10 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
/// ```
/// This function builds the entire call to format!.
fn build_format(&self, input: &String, span: proc_macro2::Span) -> proc_macro2::TokenStream {
let mut referenced_fields: HashSet<String> = HashSet::new();
// This set is used later to generate the final format string. To keep builds reproducible,
// the iteration order needs to be deterministic, hence why we use a BTreeSet here instead
// of a HashSet.
let mut referenced_fields: BTreeSet<String> = BTreeSet::new();

// At this point, we can start parsing the format string.
let mut it = input.chars().peekable();
Expand Down