Skip to content

Commit 4d9bc42

Browse files
authored
Provide more specific error messages for missing Items (microsoft#805)
This adds more granular error messages when attributes or visibility modifiers are present without an attribute, following the pattern introducing for the "floating" doc comment errors. ![image](https://github.com/microsoft/qsharp/assets/10567287/f6ffbebe-1ea0-43a0-a45f-a41524f5aee0) Fixes microsoft#757
1 parent c6949ea commit 4d9bc42

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

compiler/qsc_parse/src/item.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ pub(super) fn parse(s: &mut Scanner) -> Result<Box<Item>> {
3838
ty
3939
} else if let Some(callable) = opt(s, parse_callable_decl)? {
4040
Box::new(ItemKind::Callable(callable))
41+
} else if visibility.is_some() {
42+
let err_item = default(s.span(lo));
43+
s.push_error(Error(ErrorKind::FloatingVisibility(err_item.span)));
44+
return Ok(err_item);
45+
} else if !attrs.is_empty() {
46+
let err_item = default(s.span(lo));
47+
s.push_error(Error(ErrorKind::FloatingAttr(err_item.span)));
48+
return Ok(err_item);
4149
} else if doc.is_some() {
4250
let err_item = default(s.span(lo));
4351
s.push_error(Error(ErrorKind::FloatingDocComment(err_item.span)));

compiler/qsc_parse/src/item/tests.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,52 @@ fn floating_doc_comments_in_namespace() {
891891
);
892892
}
893893

894+
#[test]
895+
fn floating_attr_in_namespace() {
896+
check_vec(
897+
parse_namespaces,
898+
"namespace MyQuantumProgram { @EntryPoint() }",
899+
&expect![[r#"
900+
Namespace _id_ [0-44] (Ident _id_ [10-26] "MyQuantumProgram"):
901+
Item _id_ [29-42]:
902+
Err
903+
904+
[
905+
Error(
906+
FloatingAttr(
907+
Span {
908+
lo: 29,
909+
hi: 42,
910+
},
911+
),
912+
),
913+
]"#]],
914+
);
915+
}
916+
917+
#[test]
918+
fn floating_visibility_in_namespace() {
919+
check_vec(
920+
parse_namespaces,
921+
"namespace MyQuantumProgram { internal }",
922+
&expect![[r#"
923+
Namespace _id_ [0-39] (Ident _id_ [10-26] "MyQuantumProgram"):
924+
Item _id_ [29-37]:
925+
Err
926+
927+
[
928+
Error(
929+
FloatingVisibility(
930+
Span {
931+
lo: 29,
932+
hi: 37,
933+
},
934+
),
935+
),
936+
]"#]],
937+
);
938+
}
939+
894940
#[test]
895941
fn two_namespaces() {
896942
check_vec(

compiler/qsc_parse/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ enum ErrorKind {
4949
#[error("expected {0}, found {1}")]
5050
#[diagnostic(code("Qsc.Parse.Token"))]
5151
Token(TokenKind, TokenKind, #[label] Span),
52+
#[error("expected item after attribute")]
53+
#[diagnostic(code("Qsc.Parse.FloatingAttr"))]
54+
FloatingAttr(#[label] Span),
5255
#[error("expected item after doc comment")]
5356
#[diagnostic(code("Qsc.Parse.FloatingDocComment"))]
5457
FloatingDocComment(#[label] Span),
58+
#[error("expected item after visibility modifier")]
59+
#[diagnostic(code("Qsc.Parse.FloatingVisibility"))]
60+
FloatingVisibility(#[label] Span),
5561
#[error("expected {0}, found {1}")]
5662
#[diagnostic(code("Qsc.Parse.Rule"))]
5763
Rule(&'static str, TokenKind, #[label] Span),
@@ -78,6 +84,8 @@ impl ErrorKind {
7884
Self::MissingSemi(span) => Self::MissingSemi(span + offset),
7985
Self::MissingParens(span) => Self::MissingParens(span + offset),
8086
Self::FloatingDocComment(span) => Self::FloatingDocComment(span + offset),
87+
Self::FloatingAttr(span) => Self::FloatingAttr(span + offset),
88+
Self::FloatingVisibility(span) => Self::FloatingVisibility(span + offset),
8189
}
8290
}
8391
}

0 commit comments

Comments
 (0)