Skip to content

Feedback #1

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
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 30 additions & 26 deletions benches/bench.rs
Copy link

Choose a reason for hiding this comment

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

refactor: Move to new API

its not a refactor if its user visible.

This is also a breaking change and should be called out

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use annotate_snippets::{Level, Renderer, Snippet};
use annotate_snippets::{level::Level, AnnotationKind, Group, Renderer, Snippet};

#[divan::bench]
fn simple() -> String {
Expand All @@ -24,24 +24,26 @@ fn simple() -> String {
_ => continue,
}
}"#;
let message = Level::Error.title("mismatched types").id("E0308").snippet(
Snippet::source(source)
.line_start(51)
.origin("src/format.rs")
.annotation(
Level::Warning
.span(5..19)
.label("expected `Option<String>` because of return type"),
)
.annotation(
Level::Error
.span(26..724)
.label("expected enum `std::option::Option`"),
),
let message = Level::ERROR.message("mismatched types").id("E0308").group(
Group::new().element(
Snippet::source(source)
Copy link

Choose a reason for hiding this comment

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

Could you explain the new API in the commit or PR?

For example, why was a Group container chosen over group accepting a Vec or IntoIterator

.line_start(51)
.origin("src/format.rs")
.annotation(
AnnotationKind::Context
.span(5..19)
.label("expected `Option<String>` because of return type"),
)
.annotation(
AnnotationKind::Primary
.span(26..724)
.label("expected enum `std::option::Option`"),
),
),
);

let renderer = Renderer::plain();
let rendered = renderer.render(message).to_string();
let rendered = renderer.render(message);
rendered
}

Expand All @@ -67,19 +69,21 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
(input, span)
})
.bench_values(|(input, span)| {
let message = Level::Error.title("mismatched types").id("E0308").snippet(
Snippet::source(&input)
.fold(true)
.origin("src/format.rs")
.annotation(
Level::Warning
.span(span)
.label("expected `Option<String>` because of return type"),
),
let message = Level::ERROR.message("mismatched types").id("E0308").group(
Group::new().element(
Snippet::source(&input)
.fold(true)
.origin("src/format.rs")
.annotation(
AnnotationKind::Context
.span(span)
.label("expected `Option<String>` because of return type"),
),
),
);

let renderer = Renderer::plain();
let rendered = renderer.render(message).to_string();
let rendered = renderer.render(message);
rendered
});
}
Expand Down
37 changes: 37 additions & 0 deletions examples/custom_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use annotate_snippets::renderer::OutputTheme;
use annotate_snippets::{level::Level, AnnotationKind, Group, Renderer, Snippet};

fn main() {
let source = r#"//@ compile-flags: -Ztreat-err-as-bug
//@ failure-status: 101
//@ error-pattern: aborting due to `-Z treat-err-as-bug=1`
//@ error-pattern: [eval_static_initializer] evaluating initializer of static `C`
//@ normalize-stderr: "note: .*\n\n" -> ""
//@ normalize-stderr: "thread 'rustc' panicked.*:\n.*\n" -> ""
//@ rustc-env:RUST_BACKTRACE=0

#![crate_type = "rlib"]

pub static C: u32 = 0 - 1;
//~^ ERROR could not evaluate static initializer
"#;
let message = Level::ERROR
.text(Some("error: internal compiler error"))
.message("could not evaluate static initializer")
.id("E0080")
.group(
Group::new().element(
Snippet::source(source)
.origin("$DIR/err.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(386..391)
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
),
),
);

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
}
36 changes: 36 additions & 0 deletions examples/custom_error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions examples/custom_level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use annotate_snippets::renderer::OutputTheme;
use annotate_snippets::{level::Level, AnnotationKind, Group, Patch, Renderer, Snippet};

fn main() {
let source = r#"// Regression test for issue #114529
// Tests that we do not ICE during const eval for a
// break-with-value in contexts where it is illegal

#[allow(while_true)]
fn main() {
[(); {
while true {
break 9; //~ ERROR `break` with value from a `while` loop
};
51
}];

[(); {
while let Some(v) = Some(9) {
break v; //~ ERROR `break` with value from a `while` loop
};
51
}];

while true {
break (|| { //~ ERROR `break` with value from a `while` loop
let local = 9;
});
}
}
"#;
let message = Level::ERROR
.message("`break` with value from a `while` loop")
.id("E0571")
.group(
Group::new().element(
Snippet::source(source)
.line_start(1)
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(483..581)
.label("can only break with a value inside `loop` or breakable block"),
)
.annotation(
AnnotationKind::Context
.span(462..472)
.label("you can't `break` with a value in a `while` loop"),
),
),
)
.group(
Group::new()
.element(
Level::HELP
.text(Some("suggestion"))
.title("use `break` on its own without a value inside this `while` loop"),
)
.element(
Snippet::source(source)
.line_start(1)
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.patch(Patch::new(483..581, "break")),
),
);

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
}
62 changes: 62 additions & 0 deletions examples/custom_level.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 18 additions & 13 deletions examples/expected_type.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use annotate_snippets::{Level, Renderer, Snippet};
use annotate_snippets::{level::Level, AnnotationKind, Group, Renderer, Snippet};

fn main() {
let source = r#" annotations: vec![SourceAnnotation {
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
,
range: <22, 25>,"#;
let message = Level::Error.title("expected type, found `22`").snippet(
Snippet::source(source)
.line_start(26)
.origin("examples/footer.rs")
.fold(true)
.annotation(
Level::Error
.span(193..195)
.label("expected struct `annotate_snippets::snippet::Slice`, found reference"),
)
.annotation(Level::Info.span(34..50).label("while parsing this struct")),
);
let message =
Level::ERROR.message("expected type, found `22`").group(
Group::new().element(
Snippet::source(source)
.line_start(26)
.origin("examples/footer.rs")
.fold(true)
.annotation(AnnotationKind::Primary.span(193..195).label(
"expected struct `annotate_snippets::snippet::Slice`, found reference",
))
.annotation(
AnnotationKind::Context
.span(34..50)
.label("while parsing this struct"),
),
),
);

let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
Expand Down
20 changes: 9 additions & 11 deletions examples/expected_type.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading