Skip to content

Commit 2033e8f

Browse files
committed
test: Rustc annotation position overlaps
1 parent 475dca6 commit 2033e8f

File tree

1 file changed

+245
-1
lines changed

1 file changed

+245
-1
lines changed

tests/rustc_tests.rs

Lines changed: 245 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use annotate_snippets::{AnnotationKind, Group, Level, Origin, Padding, Patch, Renderer, Snippet};
66

77
use annotate_snippets::renderer::OutputTheme;
8-
use snapbox::{assert_data_eq, str};
8+
use snapbox::{assert_data_eq, str, IntoData};
99

1010
#[test]
1111
fn ends_on_col0() {
@@ -3445,3 +3445,247 @@ LL | let _: S::<bool>::Pr = ();
34453445
let renderer = Renderer::plain().anonymized_line_numbers(true);
34463446
assert_data_eq!(renderer.render(input), expected);
34473447
}
3448+
3449+
#[test]
3450+
fn unsafe_extern_suggestion() {
3451+
// tests/ui/rust-2024/unsafe-extern-blocks/unsafe-extern-suggestion.rs
3452+
3453+
let source = r#"//@ run-rustfix
3454+
3455+
#![deny(missing_unsafe_on_extern)]
3456+
#![allow(unused)]
3457+
3458+
extern "C" {
3459+
//~^ ERROR extern blocks should be unsafe [missing_unsafe_on_extern]
3460+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
3461+
static TEST1: i32;
3462+
fn test1(i: i32);
3463+
}
3464+
3465+
unsafe extern "C" {
3466+
static TEST2: i32;
3467+
fn test2(i: i32);
3468+
}
3469+
3470+
fn main() {}
3471+
"#;
3472+
3473+
let title_0 =
3474+
"this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!";
3475+
let title_1 = "for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-extern.html>";
3476+
3477+
let input = &[
3478+
Group::with_title(Level::ERROR.title("extern blocks should be unsafe"))
3479+
.element(
3480+
Snippet::source(source)
3481+
.path("$DIR/unsafe-extern-suggestion.rs")
3482+
.annotation(
3483+
AnnotationKind::Context
3484+
.span(71..71)
3485+
.label("help: needs `unsafe` before the extern keyword: `unsafe`"),
3486+
)
3487+
.annotation(AnnotationKind::Primary.span(71..303)),
3488+
)
3489+
.element(Level::WARNING.message(title_0))
3490+
.element(Level::NOTE.message(title_1)),
3491+
Group::with_title(Level::NOTE.title("the lint level is defined here")).element(
3492+
Snippet::source(source)
3493+
.path("$DIR/unsafe-extern-suggestion.rs")
3494+
.annotation(AnnotationKind::Primary.span(25..49)),
3495+
),
3496+
];
3497+
3498+
let expected = str![[r#"
3499+
error: extern blocks should be unsafe
3500+
--> $DIR/unsafe-extern-suggestion.rs:6:1
3501+
|
3502+
LL | extern "C" {
3503+
| ^ help: needs `unsafe` before the extern keyword: `unsafe`
3504+
| _|
3505+
| |
3506+
LL | | //~^ ERROR extern blocks should be unsafe [missing_unsafe_on_extern]
3507+
LL | | //~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
3508+
LL | | static TEST1: i32;
3509+
LL | | fn test1(i: i32);
3510+
LL | | }
3511+
| |_^
3512+
|
3513+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
3514+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-extern.html>
3515+
note: the lint level is defined here
3516+
--> $DIR/unsafe-extern-suggestion.rs:3:9
3517+
|
3518+
LL | #![deny(missing_unsafe_on_extern)]
3519+
| ^^^^^^^^^^^^^^^^^^^^^^^^
3520+
"#]];
3521+
let renderer = Renderer::plain().anonymized_line_numbers(true);
3522+
assert_data_eq!(renderer.render(input), expected);
3523+
}
3524+
3525+
#[test]
3526+
fn alloc_error_handler_bad_signature_2() {
3527+
// tests/ui/alloc-error/alloc-error-handler-bad-signature-2.rs
3528+
3529+
let source = r#"//@ compile-flags:-C panic=abort
3530+
3531+
#![feature(alloc_error_handler)]
3532+
#![no_std]
3533+
#![no_main]
3534+
3535+
struct Layout;
3536+
3537+
#[alloc_error_handler]
3538+
fn oom(
3539+
info: Layout, //~^ ERROR mismatched types
3540+
) { //~^^ ERROR mismatched types
3541+
loop {}
3542+
}
3543+
3544+
#[panic_handler]
3545+
fn panic(_: &core::panic::PanicInfo) -> ! { loop {} }
3546+
"#;
3547+
let title_0 =
3548+
"`core::alloc::Layout` and `Layout` have similar names, but are actually distinct types";
3549+
3550+
let input = &[
3551+
Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
3552+
.element(
3553+
Snippet::source(source)
3554+
.path("$DIR/alloc-error-handler-bad-signature-2.rs")
3555+
.annotation(
3556+
AnnotationKind::Primary
3557+
.span(130..230)
3558+
.label("expected `Layout`, found `core::alloc::Layout`"),
3559+
)
3560+
.annotation(
3561+
AnnotationKind::Context
3562+
.span(130..185)
3563+
.label("arguments to this function are incorrect"),
3564+
)
3565+
.annotation(
3566+
AnnotationKind::Context
3567+
.span(107..129)
3568+
.label("in this procedural macro expansion"),
3569+
),
3570+
)
3571+
.element(Level::NOTE.message(title_0)),
3572+
Group::with_title(Level::NOTE.title("`core::alloc::Layout` is defined in crate `core`"))
3573+
.element(
3574+
Origin::path("$SRC_DIR/core/src/alloc/layout.rs")
3575+
.line(40)
3576+
.char_column(0)
3577+
.primary(true),
3578+
),
3579+
Group::with_title(Level::NOTE.title("`Layout` is defined in the current crate")).element(
3580+
Snippet::source(source)
3581+
.path("$DIR/alloc-error-handler-bad-signature-2.rs")
3582+
.annotation(AnnotationKind::Primary.span(91..104)),
3583+
),
3584+
Group::with_title(Level::NOTE.title("function defined here")).element(
3585+
Snippet::source(source)
3586+
.path("$DIR/alloc-error-handler-bad-signature-2.rs")
3587+
.annotation(AnnotationKind::Context.span(142..154).label(""))
3588+
.annotation(AnnotationKind::Primary.span(133..136)),
3589+
),
3590+
];
3591+
let expected = str![[r#"
3592+
error[E0308]: mismatched types
3593+
--> $DIR/alloc-error-handler-bad-signature-2.rs:10:1
3594+
|
3595+
LL | #[alloc_error_handler]
3596+
| ---------------------- in this procedural macro expansion
3597+
LL | // fn oom(
3598+
LL | || info: Layout, //~^ ERROR mismatched types
3599+
LL | || ) { //~^^ ERROR mismatched types
3600+
| ||_- arguments to this function are incorrect
3601+
LL | | loop {}
3602+
LL | | }
3603+
| |__^ expected `Layout`, found `core::alloc::Layout`
3604+
|
3605+
= note: `core::alloc::Layout` and `Layout` have similar names, but are actually distinct types
3606+
note: `core::alloc::Layout` is defined in crate `core`
3607+
--> $SRC_DIR/core/src/alloc/layout.rs:40:0
3608+
note: `Layout` is defined in the current crate
3609+
--> $DIR/alloc-error-handler-bad-signature-2.rs:7:1
3610+
|
3611+
LL | struct Layout;
3612+
| ^^^^^^^^^^^^^
3613+
note: function defined here
3614+
--> $DIR/alloc-error-handler-bad-signature-2.rs:10:4
3615+
|
3616+
LL | fn oom(
3617+
| ^^^
3618+
LL | info: Layout, //~^ ERROR mismatched types
3619+
| ------------
3620+
"#]];
3621+
let renderer = Renderer::plain().anonymized_line_numbers(true);
3622+
assert_data_eq!(renderer.render(input), expected);
3623+
}
3624+
3625+
#[test]
3626+
fn str_escape() {
3627+
// tests/ui/str/str-escape.rs
3628+
3629+
let source = r#"//@ check-pass
3630+
// ignore-tidy-tab
3631+
//@ edition: 2021
3632+
3633+
fn main() {
3634+
let s = "\
3635+
3636+
";
3637+
//~^^^ WARNING multiple lines skipped by escaped newline
3638+
assert_eq!(s, "");
3639+
3640+
let s = c"foo\
3641+
  bar
3642+
";
3643+
//~^^^ WARNING whitespace symbol '\u{a0}' is not skipped
3644+
assert_eq!(s, c"foo  bar\n ");
3645+
3646+
let s = "a\
3647+
b";
3648+
assert_eq!(s, "ab");
3649+
3650+
let s = "a\
3651+
b";
3652+
assert_eq!(s, "ab");
3653+
3654+
let s = b"a\
3655+
3656+
b";
3657+
//~^^ WARNING whitespace symbol '\u{c}' is not skipped
3658+
// '\x0c' is ASCII whitespace, but it may not need skipped
3659+
// discussion: https://github.com/rust-lang/rust/pull/108403
3660+
assert_eq!(s, b"a\x0cb");
3661+
}
3662+
"#;
3663+
3664+
let input =
3665+
&[
3666+
Group::with_title(Level::WARNING.title(r#"whitespace symbol '\u{a0}' is not skipped"#))
3667+
.element(
3668+
Snippet::source(source)
3669+
.path("$DIR/str-escape.rs")
3670+
.annotation(
3671+
AnnotationKind::Context
3672+
.span(203..205)
3673+
.label(r#"whitespace symbol '\u{a0}' is not skipped"#),
3674+
)
3675+
.annotation(AnnotationKind::Primary.span(199..205)),
3676+
),
3677+
];
3678+
let expected = str![[r#"
3679+
warning: whitespace symbol '\u{a0}' is not skipped
3680+
--> $DIR/str-escape.rs:12:18
3681+
|
3682+
LL | let s = c"foo\
3683+
| __________________^
3684+
LL | |   bar
3685+
| | ^ whitespace symbol '\u{a0}' is not skipped
3686+
| |___|
3687+
|
3688+
"#]];
3689+
let renderer = Renderer::plain().anonymized_line_numbers(true);
3690+
assert_data_eq!(renderer.render(input), expected.raw());
3691+
}

0 commit comments

Comments
 (0)