From 57e217e81c087371b28a4bb7aac84286eef2c24d Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 16 Mar 2023 23:22:36 +0100 Subject: [PATCH] Add failing tests (fmt panic) on weird doc comments. Isolated doc comments causes the compiler to panic with: ``` 'no consecutive empty lines' ``` This is reproducible when doc comments are wrapped in sandwich between comments and newlines. --- crates/aiken-lang/src/tests/check.rs | 19 +++++++++++++++++++ crates/aiken-lang/src/tests/format.rs | 26 ++++++++++++++++++++++++++ crates/aiken-lang/src/tipo/error.rs | 7 +++++++ 3 files changed, 52 insertions(+) diff --git a/crates/aiken-lang/src/tests/check.rs b/crates/aiken-lang/src/tests/check.rs index 0bf369a7..92cb10e3 100644 --- a/crates/aiken-lang/src/tests/check.rs +++ b/crates/aiken-lang/src/tests/check.rs @@ -322,6 +322,25 @@ fn utf8_hex_literal_warning() { )) } +#[test] +fn weird_comments() { + let source_code = r#" + // A + + /// B + + // C + fn foo () { + todo + } + "#; + + assert!(matches!( + check(parse(source_code)), + Err((_, Error::UnexpectedDocComment { .. })) + )) +} + #[test] fn discarded_let_bindings() { let source_code = r#" diff --git a/crates/aiken-lang/src/tests/format.rs b/crates/aiken-lang/src/tests/format.rs index b00ab672..a71581d6 100644 --- a/crates/aiken-lang/src/tests/format.rs +++ b/crates/aiken-lang/src/tests/format.rs @@ -629,3 +629,29 @@ fn test_preserve_pipe() { assert_fmt(src, expected); } + +#[test] +fn weird_comments() { + let src = indoc! { r#" + // A + + /// B + + /// C + fn bar () { + todo + } + "#}; + + let expected = indoc! { r#" + // A + + /// B + /// C + fn bar () { + todo + } + "#}; + + assert_fmt(src, expected); +} diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index 3486256a..02cd2a81 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -892,6 +892,13 @@ The best thing to do from here is to remove it."#))] #[label("{} arguments", if *count < 2 { "not enough" } else { "too many" })] location: Span, }, + + #[error("I ran into a lonely doc comment not attached to any function definition.")] + #[diagnostic(code("unexpected::doc_comment"))] + UnexpectedDocComment { + #[label("unexpected doc comment")] + location: Span, + }, } impl Error {