Description: Port to annotate-snippets 0.12 Author: Simon Quigley Origin: vendor Forwarded: not-needed Last-Update: 2026-08-18 --- --- a/Cargo.toml +++ b/Cargo.toml @@ -88,7 +88,7 @@ name = "bindgen" path = "lib.rs" [dependencies.annotate-snippets] -version = "0.11.4" +version = "0.12" optional = true [dependencies.bitflags] --- a/codegen/mod.rs +++ b/codegen/mod.rs @@ -4861,18 +4861,18 @@ fn unsupported_abi_diagnostic( "Skipping {}function `{fn_name}` because the {error}", if variadic { "variadic " } else { "" }, ), - Level::Warning, + Level::WARNING, ) .add_annotation( "No code will be generated for this function.", - Level::Warning, + Level::WARNING, ) .add_annotation( format!( "The configured Rust version is {}.", ctx.options().rust_target ), - Level::Note, + Level::NOTE, ); if let Some(loc) = location { @@ -4908,9 +4908,9 @@ fn variadic_fn_diagnostic( let mut diag = Diagnostic::default(); - diag.with_title(format!("Cannot generate wrapper for the static function `{fn_name}`."), Level::Warning) - .add_annotation("The `--wrap-static-fns` feature does not support variadic functions.", Level::Note) - .add_annotation("No code will be generated for this function.", Level::Note); + diag.with_title(format!("Cannot generate wrapper for the static function `{fn_name}`."), Level::WARNING) + .add_annotation("The `--wrap-static-fns` feature does not support variadic functions.", Level::NOTE) + .add_annotation("No code will be generated for this function.", Level::NOTE); if let Some(loc) = _location { let (file, line, col, _) = loc.location(); --- a/diagnostics.rs +++ b/diagnostics.rs @@ -6,16 +6,16 @@ use std::fmt::Write; use std::io::{self, BufRead, BufReader}; use std::{borrow::Cow, fs::File}; -use annotate_snippets::{Renderer, Snippet}; +use annotate_snippets::{Annotation, Group, Renderer, Snippet}; pub(crate) use annotate_snippets::Level; /// A `bindgen` diagnostic. #[derive(Default)] pub(crate) struct Diagnostic<'a> { - title: Option<(Cow<'a, str>, Level)>, + title: Option<(Cow<'a, str>, Level<'static>)>, slices: Vec>, - footer: Vec<(Cow<'a, str>, Level)>, + footer: Vec<(Cow<'a, str>, Level<'static>)>, } impl<'a> Diagnostic<'a> { @@ -23,7 +23,7 @@ impl<'a> Diagnostic<'a> { pub(crate) fn with_title( &mut self, title: impl Into>, - level: Level, + level: Level<'static>, ) -> &mut Self { self.title = Some((title.into(), level)); self @@ -39,7 +39,7 @@ impl<'a> Diagnostic<'a> { pub(crate) fn add_annotation( &mut self, msg: impl Into>, - level: Level, + level: Level<'static>, ) -> &mut Self { self.footer.push((msg.into(), level)); self @@ -54,37 +54,32 @@ impl<'a> Diagnostic<'a> { static INVOKED_BY_BUILD_SCRIPT: bool = std::env::var_os("CARGO_CFG_TARGET_ARCH").is_some(); } - let mut footer = vec![]; - let mut slices = vec![]; - let snippet = if let Some((msg, level)) = &self.title { - (*level).title(msg) - } else { + let Some((msg, level)) = &self.title else { return; }; - for (msg, level) in &self.footer { - footer.push((*level).title(msg)); - } - - // add additional info that this is generated by bindgen - // so as to not confuse with rustc warnings - footer.push( - Level::Info.title("This diagnostic was generated by bindgen."), - ); + let mut group = Group::with_title(level.clone().primary_title(msg.as_ref())); for slice in &self.slices { if let Some(source) = &slice.source { - let mut snippet = Snippet::source(source) + let mut snippet = Snippet::::source(source.as_ref()) .line_start(slice.line.unwrap_or_default()); if let Some(origin) = &slice.filename { - snippet = snippet.origin(origin); + snippet = snippet.path(origin.as_str()); } - slices.push(snippet); + group = group.element(snippet); } } + for (msg, level) in &self.footer { + group = group.element(level.clone().message(msg.as_ref())); + } + // add additional info that this is generated by bindgen + // so as to not confuse with rustc warnings + group = group.element(Level::INFO.message("This diagnostic was generated by bindgen.")); + let renderer = Renderer::styled(); - let dl = renderer.render(snippet.snippets(slices).footers(footer)); + let dl = renderer.render(std::slice::from_ref(&group)); if INVOKED_BY_BUILD_SCRIPT.with(Clone::clone) { // This is just a hack which hides the `warning:` added by cargo at the beginning of --- a/ir/context.rs +++ b/ir/context.rs @@ -3096,11 +3096,11 @@ fn unused_regex_diagnostic(item: &str, n Diagnostic::default() .with_title( format!("Unused regular expression: `{item}`."), - Level::Warning, + Level::WARNING, ) .add_annotation( format!("This regular expression was passed to `{name}`."), - Level::Note, + Level::NOTE, ) .display(); } --- a/ir/var.rs +++ b/ir/var.rs @@ -515,9 +515,9 @@ fn duplicated_macro_diagnostic( slice.with_source(source); Diagnostic::default() - .with_title("Duplicated macro definition.", Level::Warning) + .with_title("Duplicated macro definition.", Level::WARNING) .add_slice(slice) - .add_annotation("This macro had a duplicate.", Level::Note) + .add_annotation("This macro had a duplicate.", Level::NOTE) .display(); } } --- a/lib.rs +++ b/lib.rs @@ -1102,10 +1102,10 @@ fn rustfmt_non_fatal_error_diagnostic(ms use crate::diagnostics::{Diagnostic, Level}; Diagnostic::default() - .with_title(msg, Level::Warning) + .with_title(msg, Level::WARNING) .add_annotation( "The bindings will be generated but not formatted.", - Level::Note, + Level::NOTE, ) .display(); } --- a/regex_set.rs +++ b/regex_set.rs @@ -164,9 +164,9 @@ fn invalid_regex_warning( let error = "error: "; if line.starts_with(error) { let (_, msg) = line.split_at(error.len()); - diagnostic.add_annotation(msg.to_owned(), Level::Error); + diagnostic.add_annotation(msg.to_owned(), Level::ERROR); } else { - diagnostic.add_annotation(line.to_owned(), Level::Info); + diagnostic.add_annotation(line.to_owned(), Level::INFO); } } let mut slice = Slice::default(); @@ -175,25 +175,25 @@ fn invalid_regex_warning( diagnostic.with_title( "Error while parsing a regular expression.", - Level::Warning, + Level::WARNING, ); } else { - diagnostic.with_title(string, Level::Warning); + diagnostic.with_title(string, Level::WARNING); } } err => { let err = err.to_string(); - diagnostic.with_title(err, Level::Warning); + diagnostic.with_title(err, Level::WARNING); } } diagnostic.add_annotation( format!("This regular expression was passed via `{name}`."), - Level::Note, + Level::NOTE, ); if set.items.iter().any(|item| item.as_ref() == "*") { - diagnostic.add_annotation("Wildcard patterns \"*\" are no longer considered valid. Use \".*\" instead.", Level::Help); + diagnostic.add_annotation("Wildcard patterns \"*\" are no longer considered valid. Use \".*\" instead.", Level::HELP); } diagnostic.display(); }