Skip to content

Commit

Permalink
Fixes qualified call path without as trait.
Browse files Browse the repository at this point in the history
The parser no longer parses qualified call path without the `as` token and trait.

This invalidates code that was previously accepted such as:
`let x: <u8>::S::<u8> = S::<u8>{x: 8};``

Fixes #6389
  • Loading branch information
esdrubal committed Nov 29, 2024
1 parent 64d1c6e commit 14744a8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sway-ast/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ impl Spanned for PathTypeSegment {
#[derive(Clone, Debug, Serialize)]
pub struct QualifiedPathRoot {
pub ty: Box<Ty>,
pub as_trait: Option<(AsToken, Box<PathType>)>,
pub as_trait: (AsToken, Box<PathType>),
}
28 changes: 12 additions & 16 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3333,22 +3333,18 @@ fn path_root_opt_to_bool_and_qualified_path_root(
close_angle_bracket_token: _,
}),
_,
)) => (
false,
if let Some((_, path_type)) = as_trait {
Some(QualifiedPathType {
ty: ty_to_type_argument(context, handler, engines, *ty)?,
as_trait: engines.te().insert(
engines,
path_type_to_type_info(context, handler, engines, *path_type.clone())?,
path_type.span().source_id(),
),
as_trait_span: path_type.span(),
})
} else {
None
},
),
)) => (false, {
let (_, path_type) = as_trait;
Some(QualifiedPathType {
ty: ty_to_type_argument(context, handler, engines, *ty)?,
as_trait: engines.te().insert(
engines,
path_type_to_type_info(context, handler, engines, *path_type.clone())?,
path_type.span().source_id(),
),
as_trait_span: path_type.span(),
})
}),
})
}

Expand Down
5 changes: 1 addition & 4 deletions sway-parse/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ impl Parse for PathTypeSegment {
impl Parse for QualifiedPathRoot {
fn parse(parser: &mut Parser) -> ParseResult<QualifiedPathRoot> {
let ty = parser.parse()?;
let as_trait = match parser.take() {
Some(as_token) => Some((as_token, parser.parse()?)),
None => None,
};
let as_trait = (parser.parse()?, parser.parse()?);
Ok(QualifiedPathRoot { ty, as_trait })
}
}
Expand Down
7 changes: 3 additions & 4 deletions swayfmt/src/utils/language/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ impl Format for QualifiedPathRoot {
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
self.ty.format(formatted_code, formatter)?;
if let Some((as_token, path_type)) = &self.as_trait {
write!(formatted_code, " {} ", as_token.span().as_str())?;
path_type.format(formatted_code, formatter)?;
}
let (as_token, path_type) = &self.as_trait;
write!(formatted_code, " {} ", as_token.span().as_str())?;
path_type.format(formatted_code, formatter)?;

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-3A3AB9F18BDA9100"

[[package]]
name = "parser_generic_turbo_fish_prefix"
source = "member"
dependencies = ["std"]

[[package]]
name = "std"
source = "path+from-root-3A3AB9F18BDA9100"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "parser_generic_turbo_fish_prefix"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script;

struct S<T> {
x: T,
}

fn main() {
let x: <u8>::S::<u8> = S::<u8>{x: 8};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

# check: $()let x: <u8>::S::<u8> = S::<u8>{x: 8};
# nextln: $()Expected `as`.

0 comments on commit 14744a8

Please sign in to comment.