Skip to content

Commit 5cc5a3c

Browse files
committed
test: Add rustc tests for pointing at end of line
1 parent ab692e6 commit 5cc5a3c

File tree

1 file changed

+352
-0
lines changed

1 file changed

+352
-0
lines changed

tests/rustc_tests.rs

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,3 +2111,355 @@ LL + x.iter_mut().for_each(|items| {
21112111
let renderer = Renderer::plain().anonymized_line_numbers(true);
21122112
assert_data_eq!(renderer.render(input), expected);
21132113
}
2114+
2115+
#[test]
2116+
fn bad_char_literals() {
2117+
// tests/ui/parser/bad-char-literals.rs
2118+
2119+
let source = r#"// ignore-tidy-cr
2120+
// ignore-tidy-tab
2121+
2122+
fn main() {
2123+
// these literals are just silly.
2124+
''';
2125+
//~^ ERROR: character constant must be escaped: `'`
2126+
2127+
// note that this is a literal "\n" byte
2128+
'
2129+
';
2130+
//~^^ ERROR: character constant must be escaped: `\n`
2131+
2132+
// note that this is a literal "\r" byte
2133+
; //~ ERROR: character constant must be escaped: `\r`
2134+
2135+
// note that this is a literal NULL
2136+
'--'; //~ ERROR: character literal may only contain one codepoint
2137+
2138+
// note that this is a literal tab character here
2139+
' ';
2140+
//~^ ERROR: character constant must be escaped: `\t`
2141+
}
2142+
"#;
2143+
2144+
let input = Level::ERROR
2145+
.header("character constant must be escaped: `\\n`")
2146+
.group(
2147+
Group::new().element(
2148+
Snippet::source(source)
2149+
.origin("$DIR/bad-char-literals.rs")
2150+
.fold(true)
2151+
.annotation(AnnotationKind::Primary.span(204..205)),
2152+
),
2153+
)
2154+
.group(
2155+
Group::new()
2156+
.element(Level::HELP.title("escape the character"))
2157+
.element(
2158+
Snippet::source(source)
2159+
.origin("$DIR/bad-char-literals.rs")
2160+
.line_start(1)
2161+
.fold(true)
2162+
.patch(Patch::new(204..205, r#"\n"#)),
2163+
),
2164+
);
2165+
let expected = str![[r#"
2166+
error: character constant must be escaped: `/n`
2167+
--> $DIR/bad-char-literals.rs:10:6
2168+
|
2169+
LL | '
2170+
| ^
2171+
|
2172+
help: escape the character
2173+
|
2174+
LL | '/n
2175+
| ++
2176+
"#]];
2177+
let renderer = Renderer::plain().anonymized_line_numbers(true);
2178+
assert_data_eq!(renderer.render(input), expected);
2179+
}
2180+
2181+
#[test]
2182+
fn unclosed_1() {
2183+
// tests/ui/frontmatter/unclosed-1.rs
2184+
2185+
let source = r#"----cargo
2186+
//~^ ERROR: unclosed frontmatter
2187+
2188+
// This test checks that the #! characters can help us recover a frontmatter
2189+
// close. There should not be a "missing `main` function" error as the rest
2190+
// are properly parsed.
2191+
2192+
#![feature(frontmatter)]
2193+
2194+
fn main() {}
2195+
"#;
2196+
2197+
let input = Level::ERROR
2198+
.header("unclosed frontmatter")
2199+
.group(
2200+
Group::new().element(
2201+
Snippet::source(source)
2202+
.origin("$DIR/unclosed-1.rs")
2203+
.fold(true)
2204+
.annotation(AnnotationKind::Primary.span(0..221)),
2205+
),
2206+
)
2207+
.group(
2208+
Group::new()
2209+
.element(Level::NOTE.title("frontmatter opening here was not closed"))
2210+
.element(
2211+
Snippet::source(source)
2212+
.origin("$DIR/unclosed-1.rs")
2213+
.fold(true)
2214+
.annotation(AnnotationKind::Primary.span(0..4)),
2215+
),
2216+
);
2217+
let expected = str![[r#"
2218+
error: unclosed frontmatter
2219+
--> $DIR/unclosed-1.rs:1:1
2220+
|
2221+
LL | / ----cargo
2222+
... |
2223+
LL | | // are properly parsed.
2224+
| |________________________^
2225+
|
2226+
note: frontmatter opening here was not closed
2227+
--> $DIR/unclosed-1.rs:1:1
2228+
|
2229+
LL | ----cargo
2230+
| ^^^^
2231+
"#]];
2232+
let renderer = Renderer::plain().anonymized_line_numbers(true);
2233+
assert_data_eq!(renderer.render(input), expected);
2234+
}
2235+
2236+
#[test]
2237+
fn unclosed_2() {
2238+
// tests/ui/frontmatter/unclosed-2.rs
2239+
2240+
let source = r#"----cargo
2241+
//~^ ERROR: unclosed frontmatter
2242+
//~| ERROR: frontmatters are experimental
2243+
2244+
//@ compile-flags: --crate-type lib
2245+
2246+
// Leading whitespace on the feature line prevents recovery. However
2247+
// the dashes quoted will not be used for recovery and the entire file
2248+
// should be treated as within the frontmatter block.
2249+
2250+
#![feature(frontmatter)]
2251+
2252+
fn foo() -> &str {
2253+
"----"
2254+
}
2255+
"#;
2256+
2257+
let input = Level::ERROR
2258+
.header("unclosed frontmatter")
2259+
.group(
2260+
Group::new().element(
2261+
Snippet::source(source)
2262+
.origin("$DIR/unclosed-2.rs")
2263+
.fold(true)
2264+
.annotation(AnnotationKind::Primary.span(0..377)),
2265+
),
2266+
)
2267+
.group(
2268+
Group::new()
2269+
.element(Level::NOTE.title("frontmatter opening here was not closed"))
2270+
.element(
2271+
Snippet::source(source)
2272+
.origin("$DIR/unclosed-2.rs")
2273+
.fold(true)
2274+
.annotation(AnnotationKind::Primary.span(0..4)),
2275+
),
2276+
);
2277+
let expected = str![[r#"
2278+
error: unclosed frontmatter
2279+
--> $DIR/unclosed-2.rs:1:1
2280+
|
2281+
LL | / ----cargo
2282+
... |
2283+
LL | | "----"
2284+
LL | | }
2285+
| |__^
2286+
|
2287+
note: frontmatter opening here was not closed
2288+
--> $DIR/unclosed-2.rs:1:1
2289+
|
2290+
LL | ----cargo
2291+
| ^^^^
2292+
"#]];
2293+
let renderer = Renderer::plain().anonymized_line_numbers(true);
2294+
assert_data_eq!(renderer.render(input), expected);
2295+
}
2296+
2297+
#[test]
2298+
fn unclosed_3() {
2299+
// tests/ui/frontmatter/unclosed-3.rs
2300+
2301+
let source = r#"----cargo
2302+
//~^ ERROR: frontmatter close does not match the opening
2303+
2304+
//@ compile-flags: --crate-type lib
2305+
2306+
// Unfortunate recovery situation. Not really preventable with improving the
2307+
// recovery strategy, but this type of code is rare enough already.
2308+
2309+
#![feature(frontmatter)]
2310+
2311+
fn foo(x: i32) -> i32 {
2312+
---x
2313+
//~^ ERROR: invalid preceding whitespace for frontmatter close
2314+
//~| ERROR: extra characters after frontmatter close are not allowed
2315+
}
2316+
//~^ ERROR: unexpected closing delimiter: `}`
2317+
"#;
2318+
2319+
let input = Level::ERROR
2320+
.header("invalid preceding whitespace for frontmatter close")
2321+
.group(
2322+
Group::new().element(
2323+
Snippet::source(source)
2324+
.origin("$DIR/unclosed-3.rs")
2325+
.fold(true)
2326+
.annotation(AnnotationKind::Primary.span(302..310)),
2327+
),
2328+
)
2329+
.group(
2330+
Group::new()
2331+
.element(
2332+
Level::NOTE.title("frontmatter close should not be preceded by whitespace"),
2333+
)
2334+
.element(
2335+
Snippet::source(source)
2336+
.origin("$DIR/unclosed-3.rs")
2337+
.fold(true)
2338+
.annotation(AnnotationKind::Primary.span(302..306)),
2339+
),
2340+
);
2341+
let expected = str![[r#"
2342+
error: invalid preceding whitespace for frontmatter close
2343+
--> $DIR/unclosed-3.rs:12:1
2344+
|
2345+
LL | ---x
2346+
| ^^^^^^^^
2347+
|
2348+
note: frontmatter close should not be preceded by whitespace
2349+
--> $DIR/unclosed-3.rs:12:1
2350+
|
2351+
LL | ---x
2352+
| ^^^^
2353+
"#]];
2354+
let renderer = Renderer::plain().anonymized_line_numbers(true);
2355+
assert_data_eq!(renderer.render(input), expected);
2356+
}
2357+
2358+
#[test]
2359+
fn unclosed_4() {
2360+
// tests/ui/frontmatter/unclosed-4.rs
2361+
2362+
let source = r#"----cargo
2363+
//~^ ERROR: unclosed frontmatter
2364+
2365+
//! Similarly, a module-level content should allow for recovery as well (as
2366+
//! per unclosed-1.rs)
2367+
2368+
#![feature(frontmatter)]
2369+
2370+
fn main() {}
2371+
"#;
2372+
2373+
let input = Level::ERROR
2374+
.header("unclosed frontmatter")
2375+
.group(
2376+
Group::new().element(
2377+
Snippet::source(source)
2378+
.origin("$DIR/unclosed-4.rs")
2379+
.fold(true)
2380+
.annotation(AnnotationKind::Primary.span(0..43)),
2381+
),
2382+
)
2383+
.group(
2384+
Group::new()
2385+
.element(Level::NOTE.title("frontmatter opening here was not closed"))
2386+
.element(
2387+
Snippet::source(source)
2388+
.origin("$DIR/unclosed-4.rs")
2389+
.fold(true)
2390+
.annotation(AnnotationKind::Primary.span(0..4)),
2391+
),
2392+
);
2393+
let expected = str![[r#"
2394+
error: unclosed frontmatter
2395+
--> $DIR/unclosed-4.rs:1:1
2396+
|
2397+
LL | / ----cargo
2398+
LL | | //~^ ERROR: unclosed frontmatter
2399+
| |_________________________________^
2400+
|
2401+
note: frontmatter opening here was not closed
2402+
--> $DIR/unclosed-4.rs:1:1
2403+
|
2404+
LL | ----cargo
2405+
| ^^^^
2406+
"#]];
2407+
let renderer = Renderer::plain().anonymized_line_numbers(true);
2408+
assert_data_eq!(renderer.render(input), expected);
2409+
}
2410+
2411+
#[test]
2412+
fn unclosed_5() {
2413+
// tests/ui/frontmatter/unclosed-5.rs
2414+
2415+
let source = r#"----cargo
2416+
//~^ ERROR: unclosed frontmatter
2417+
//~| ERROR: frontmatters are experimental
2418+
2419+
// Similarly, a use statement should allow for recovery as well (as
2420+
// per unclosed-1.rs)
2421+
2422+
use std::env;
2423+
2424+
fn main() {}
2425+
"#;
2426+
2427+
let input = Level::ERROR
2428+
.header("unclosed frontmatter")
2429+
.group(
2430+
Group::new().element(
2431+
Snippet::source(source)
2432+
.origin("$DIR/unclosed-5.rs")
2433+
.fold(true)
2434+
.annotation(AnnotationKind::Primary.span(0..176)),
2435+
),
2436+
)
2437+
.group(
2438+
Group::new()
2439+
.element(Level::NOTE.title("frontmatter opening here was not closed"))
2440+
.element(
2441+
Snippet::source(source)
2442+
.origin("$DIR/unclosed-5.rs")
2443+
.fold(true)
2444+
.annotation(AnnotationKind::Primary.span(0..4)),
2445+
),
2446+
);
2447+
2448+
let expected = str![[r#"
2449+
error: unclosed frontmatter
2450+
--> $DIR/unclosed-5.rs:1:1
2451+
|
2452+
LL | / ----cargo
2453+
... |
2454+
LL | | // per unclosed-1.rs)
2455+
| |______________________^
2456+
|
2457+
note: frontmatter opening here was not closed
2458+
--> $DIR/unclosed-5.rs:1:1
2459+
|
2460+
LL | ----cargo
2461+
| ^^^^
2462+
"#]];
2463+
let renderer = Renderer::plain().anonymized_line_numbers(true);
2464+
assert_data_eq!(renderer.render(input), expected);
2465+
}

0 commit comments

Comments
 (0)