Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 20 additions & 2 deletions crates/parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ impl<'sess, 'ast> Parser<'sess, 'ast> {
while let Some((is_doc, kind, symbol)) = self.token.comment() {
if is_doc {
let natspec = if let Some(items) =
parse_natspec(self.token.span, symbol, self.in_yul, self.dcx())
parse_natspec(self.token.span, symbol, kind, self.in_yul, self.dcx())
{
self.alloc_smallvec(items)
} else {
Expand Down Expand Up @@ -1039,6 +1039,7 @@ impl<'sess, 'ast> Parser<'sess, 'ast> {
fn parse_natspec(
comment_span: Span,
comment_symbol: Symbol,
comment_kind: ast::CommentKind,
in_yul: bool,
dcx: &DiagCtxt,
) -> Option<SmallVec<[ast::NatSpecItem; 6]>> {
Expand Down Expand Up @@ -1084,10 +1085,27 @@ fn parse_natspec(
}
}

// Check if '@' is located at the logical start of the line.
let is_line_start = |line_start: usize, at_pos: usize| {
match comment_kind {
// can only be followed by whitespace
ast::CommentKind::Line => bytes[line_start..at_pos].trim_ascii().is_empty(),

// can only be followed by whitespaces + ('*') + whitespaces
ast::CommentKind::Block => {
let trimmed = &bytes[line_start..at_pos].trim_ascii_start();
trimmed.is_empty()
|| (trimmed.starts_with(b"*") && trimmed[1..].trim_ascii_end().is_empty())
}
}
};

// Iterate over each line and look for tags.
let mut prev_line_end = 0;
for line_end in memchr::memchr_iter(b'\n', bytes).chain(std::iter::once(bytes.len())) {
if let Some(tag_offset) = memchr::memchr(b'@', &bytes[line_start..line_end]) {
if let Some(tag_offset) = memchr::memchr(b'@', &bytes[line_start..line_end])
&& is_line_start(line_start, line_start + tag_offset)
{
let tag_start = line_start + tag_offset + 1;
flush_item(&mut items, &mut kind, &mut span, content_start, prev_line_end);

Expand Down
11 changes: 10 additions & 1 deletion tests/ui/natspec/natspec.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,13 @@ contract YulContext {
}

/// @ author whitespace before tag is valid
contract Repro {}
/// @notice it is valid to add '@'s in the middle of a comment
/// * Solve: 2yr @ 5% == 1yr @ 0% + 1yr @ x => x = 10.00%
contract ReproLineComment {}

/**
* @ author whitespace before tag is valid
* @notice it is valid to add '@'s in the middle of a comment
* Solve: 2yr @ 5% == 1yr @ 0% + 1yr @ x => x = 10.00%
**/
contract ReproBlockComment {}
Loading