Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix span placement for IDs with type hints #338

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
51 changes: 34 additions & 17 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,11 @@ impl<'source> Parser<'source> {
match self.parse_id_or_wildcard(context)? {
Some(IdOrWildcard::Id(constant_index)) => {
arg_ids.push(constant_index);
let arg_span = self.current_span();
let type_hint = self.parse_type_hint(&args_context)?;
arg_nodes.push(self.push_node(Node::Id(constant_index, type_hint))?);
arg_nodes.push(
self.push_node_with_span(Node::Id(constant_index, type_hint), arg_span)?,
);

if self.peek_token() == Some(Token::Ellipsis) {
if type_hint.is_some() {
Expand All @@ -903,8 +906,11 @@ impl<'source> Parser<'source> {
}
}
Some(IdOrWildcard::Wildcard(maybe_id)) => {
let arg_span = self.current_span();
let type_hint = self.parse_type_hint(&args_context)?;
arg_nodes.push(self.push_node(Node::Wildcard(maybe_id, type_hint))?);
arg_nodes.push(
self.push_node_with_span(Node::Wildcard(maybe_id, type_hint), arg_span)?,
);
}
None => match self.peek_token() {
Some(Token::Self_) => {
Expand Down Expand Up @@ -1051,19 +1057,23 @@ impl<'source> Parser<'source> {
return self.error(SyntaxError::SelfArg);
}

let arg_span = self.current_span();
let arg_node = if self.peek_token() == Some(Token::Ellipsis) {
self.consume_token();
Node::Ellipsis(Some(constant_index))
} else {
Node::Id(constant_index, self.parse_type_hint(&args_context)?)
};

nested_args.push(self.push_node(arg_node)?);
nested_args.push(self.push_node_with_span(arg_node, arg_span)?);
arg_ids.push(constant_index);
}
Some(IdOrWildcard::Wildcard(maybe_id)) => {
let arg_span = self.current_span();
let type_hint = self.parse_type_hint(&args_context)?;
nested_args.push(self.push_node(Node::Wildcard(maybe_id, type_hint))?);
nested_args.push(
self.push_node_with_span(Node::Wildcard(maybe_id, type_hint), arg_span)?,
);
}
None => match self.peek_token() {
Some(Token::RoundOpen) => {
Expand Down Expand Up @@ -2061,17 +2071,17 @@ impl<'source> Parser<'source> {

let mut args = AstVec::new();
while let Some(id_or_wildcard) = self.parse_id_or_wildcard(context)? {
let arg_span = self.current_span();
let type_hint = self.parse_type_hint(context)?;

match id_or_wildcard {
let arg_node = match id_or_wildcard {
IdOrWildcard::Id(id) => {
self.frame_mut()?.ids_assigned_in_frame.insert(id);
args.push(self.push_node(Node::Id(id, type_hint))?);
}
IdOrWildcard::Wildcard(maybe_id) => {
args.push(self.push_node(Node::Wildcard(maybe_id, type_hint))?);
Node::Id(id, type_hint)
}
}
IdOrWildcard::Wildcard(maybe_id) => Node::Wildcard(maybe_id, type_hint),
};
args.push(self.push_node_with_span(arg_node, arg_span)?);

match self.peek_next_token_on_same_line() {
Some(Token::Comma) => {
Expand Down Expand Up @@ -2496,8 +2506,10 @@ impl<'source> Parser<'source> {
.error(SyntaxError::MatchEllipsisOutsideOfNestedPatterns);
}
} else {
let id_span = self.current_span();
let type_hint = self.parse_type_hint(&pattern_context)?;
let id_node = self.push_node(Node::Id(id, type_hint))?;
let id_node =
self.push_node_with_span(Node::Id(id, type_hint), id_span)?;
if self.next_token_is_chain_start(&pattern_context) {
self.frame_mut()?.add_id_access(id);
self.consume_chain(id_node, &pattern_context)?
Expand All @@ -2511,9 +2523,12 @@ impl<'source> Parser<'source> {
None => return self.error(InternalError::IdParseFailure),
},
Wildcard => {
let maybe_id = self.consume_wildcard(&pattern_context)?;
let wildcard = self.consume_wildcard(&pattern_context)?;
let wildcard_span = self.current_span();
let maybe_type = self.parse_type_hint(&pattern_context)?;
Some(self.push_node(Node::Wildcard(maybe_id, maybe_type))?)
let result = self
.push_node_with_span(Node::Wildcard(wildcard, maybe_type), wildcard_span)?;
Some(result)
}
RoundOpen => {
self.consume_token_with_context(&pattern_context);
Expand Down Expand Up @@ -2753,16 +2768,18 @@ impl<'source> Parser<'source> {
while let Some(id_or_wildcard) =
self.parse_id_or_wildcard(&ExpressionContext::permissive())?
{
match id_or_wildcard {
let target_span = self.current_span();
let target_node = match id_or_wildcard {
IdOrWildcard::Id(constant_index) => {
let type_hint_index = self.parse_type_hint(context)?;
targets.push(self.push_node(Node::Id(constant_index, type_hint_index))?);
Node::Id(constant_index, type_hint_index)
}
IdOrWildcard::Wildcard(maybe_id) => {
let type_hint_index = self.parse_type_hint(context)?;
targets.push(self.push_node(Node::Wildcard(maybe_id, type_hint_index))?);
Node::Wildcard(maybe_id, type_hint_index)
}
}
};
targets.push(self.push_node_with_span(target_node, target_span)?);

if let Some(Token::Comma) = self
.peek_token_with_context(context)
Expand Down
Loading