|
| 1 | +use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet}; |
| 2 | +use anstyle::Effects; |
| 3 | + |
| 4 | +fn main() { |
| 5 | + let source = r#"// Make sure "highlighted" code is colored purple |
| 6 | +
|
| 7 | +//@ compile-flags: --error-format=human --color=always |
| 8 | +//@ error-pattern:[35mfor<'a> [0m |
| 9 | +//@ edition:2018 |
| 10 | +
|
| 11 | +use core::pin::Pin; |
| 12 | +use core::future::Future; |
| 13 | +use core::any::Any; |
| 14 | +
|
| 15 | +fn query(_: fn(Box<(dyn Any + Send + '_)>) -> Pin<Box<( |
| 16 | + dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static |
| 17 | +)>>) {} |
| 18 | +
|
| 19 | +fn wrapped_fn<'a>(_: Box<(dyn Any + Send)>) -> Pin<Box<( |
| 20 | + dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static |
| 21 | +)>> { |
| 22 | + Box::pin(async { Err("nope".into()) }) |
| 23 | +} |
| 24 | +
|
| 25 | +fn main() { |
| 26 | + query(wrapped_fn); |
| 27 | +} |
| 28 | +"#; |
| 29 | + |
| 30 | + let magenta = annotate_snippets::renderer::AnsiColor::Magenta |
| 31 | + .on_default() |
| 32 | + .effects(Effects::BOLD); |
| 33 | + let title = format!( |
| 34 | + "expected fn pointer `{}for<'a>{} fn(Box<{}(dyn Any + Send + 'a){}>) -> Pin<_>` |
| 35 | + found fn item `fn(Box<{}(dyn Any + Send + 'static){}>) -> Pin<_> {}{{wrapped_fn}}{}`", |
| 36 | + magenta.render(), |
| 37 | + magenta.render_reset(), |
| 38 | + magenta.render(), |
| 39 | + magenta.render_reset(), |
| 40 | + magenta.render(), |
| 41 | + magenta.render_reset(), |
| 42 | + magenta.render(), |
| 43 | + magenta.render_reset() |
| 44 | + ); |
| 45 | + |
| 46 | + let message = Level::Error.message("mismatched types").id("E0308").group( |
| 47 | + Group::new() |
| 48 | + .element( |
| 49 | + Snippet::source(source) |
| 50 | + .fold(true) |
| 51 | + .origin("$DIR/highlighting.rs") |
| 52 | + .annotation( |
| 53 | + AnnotationKind::Primary |
| 54 | + .span(589..599) |
| 55 | + .label("one type is more general than the other"), |
| 56 | + ) |
| 57 | + .annotation( |
| 58 | + AnnotationKind::Context |
| 59 | + .span(583..588) |
| 60 | + .label("arguments to this function are incorrect"), |
| 61 | + ), |
| 62 | + ) |
| 63 | + .element(Level::Note.title(&title)), |
| 64 | + ); |
| 65 | + |
| 66 | + let renderer = Renderer::styled().anonymized_line_numbers(true); |
| 67 | + anstream::println!("{}", renderer.render(message)); |
| 68 | +} |
0 commit comments