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: distinguish TypePath with and without turbofish #6404

Merged
merged 3 commits into from
Oct 30, 2024
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
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ impl Display for AsTraitPath {
impl Display for TypePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}::{}", self.typ, self.item)?;
if !self.turbofish.is_empty() {
write!(f, "::{}", self.turbofish)?;
if let Some(turbofish) = &self.turbofish {
write!(f, "::{}", turbofish)?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ pub struct AsTraitPath {
pub struct TypePath {
pub typ: UnresolvedType,
pub item: Ident,
pub turbofish: GenericTypeArgs,
pub turbofish: Option<GenericTypeArgs>,
}

// Note: Path deliberately doesn't implement Recoverable.
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,9 @@

pub fn accept_children(&self, visitor: &mut impl Visitor) {
self.typ.accept(visitor);
self.turbofish.accept(visitor);
if let Some(turbofish) = &self.turbofish {
turbofish.accept(visitor);
}
}
}

Expand Down Expand Up @@ -1318,8 +1320,8 @@
UnresolvedTypeData::Unspecified => visitor.visit_unspecified_type(self.span),
UnresolvedTypeData::Quoted(typ) => visitor.visit_quoted_type(typ, self.span),
UnresolvedTypeData::FieldElement => visitor.visit_field_element_type(self.span),
UnresolvedTypeData::Integer(signdness, size) => {

Check warning on line 1323 in compiler/noirc_frontend/src/ast/visitor.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (signdness)
visitor.visit_integer_type(*signdness, *size, self.span);

Check warning on line 1324 in compiler/noirc_frontend/src/ast/visitor.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (signdness)
}
UnresolvedTypeData::Bool => visitor.visit_bool_type(self.span),
UnresolvedTypeData::Unit => visitor.visit_unit_type(self.span),
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,8 @@ impl<'context> Elaborator<'context> {
.func_id(self.interner)
.expect("Expected trait function to be a DefinitionKind::Function");

let generics = self.resolve_type_args(path.turbofish, func_id, span).0;
let generics = (!generics.is_empty()).then_some(generics);
let generics =
path.turbofish.map(|turbofish| self.resolve_type_args(turbofish, func_id, span).0);

let location = Location::new(span, self.file);
let id = self.interner.function_definition_id(func_id);
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/hir/comptime/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,9 @@ fn remove_interned_in_expression_kind(
}
ExpressionKind::TypePath(mut path) => {
path.typ = remove_interned_in_unresolved_type(interner, path.typ);
path.turbofish = remove_interned_in_generic_type_args(interner, path.turbofish);
path.turbofish = path
.turbofish
.map(|turbofish| remove_interned_in_generic_type_args(interner, turbofish));
ExpressionKind::TypePath(path)
}
ExpressionKind::Resolved(id) => {
Expand Down
12 changes: 5 additions & 7 deletions compiler/noirc_frontend/src/parser/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{
ast::{
ArrayLiteral, BlockExpression, CallExpression, CastExpression, ConstructorExpression,
Expression, ExpressionKind, GenericTypeArgs, Ident, IfExpression, IndexExpression, Literal,
Expression, ExpressionKind, Ident, IfExpression, IndexExpression, Literal,
MemberAccessExpression, MethodCallExpression, Statement, TypePath, UnaryOp, UnresolvedType,
},
parser::{labels::ParsingRuleLabel, parser::parse_many::separated_by_comma, ParserErrorReason},
Expand Down Expand Up @@ -548,15 +548,13 @@
Ident::new(String::new(), self.span_at_previous_token_end())
};

let turbofish = if self.eat_double_colon() {
let turbofish = self.eat_double_colon().then(|| {
let generics = self.parse_generic_type_args();
if generics.is_empty() {
self.expected_token(Token::Less);
}
generics
} else {
GenericTypeArgs::default()
};
});

Some(ExpressionKind::TypePath(TypePath { typ, item, turbofish }))
}
Expand All @@ -565,7 +563,7 @@
/// = bool
/// | int
/// | str
/// | rawstr

Check warning on line 566 in compiler/noirc_frontend/src/parser/parser/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (rawstr)
/// | fmtstr
/// | QuoteExpression
/// | ArrayExpression
Expand Down Expand Up @@ -1587,7 +1585,7 @@
};
assert_eq!(type_path.typ.to_string(), "Field");
assert_eq!(type_path.item.to_string(), "foo");
assert!(type_path.turbofish.is_empty());
assert!(type_path.turbofish.is_none());
}

#[test]
Expand All @@ -1599,7 +1597,7 @@
};
assert_eq!(type_path.typ.to_string(), "Field");
assert_eq!(type_path.item.to_string(), "foo");
assert!(!type_path.turbofish.is_empty());
assert!(type_path.turbofish.is_some());
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions test_programs/compile_success_empty/type_path/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ fn main() {
$foo::static()
}
}

// Make sure this call works fine: in the past we used to not distinguish
// whether a TypePath had generics or not, always resolved them, filling them
// up with Type::Error, and eventually leading to an ICE.
let _ = Field::from_be_bytes([1]);
}

pub struct Foo {}
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@
formatter.format_type(type_path.typ);
formatter.write_token(Token::DoubleColon);
formatter.write_identifier(type_path.item);
if !type_path.turbofish.is_empty() {
if let Some(turbofish) = type_path.turbofish {
formatter.write_token(Token::DoubleColon);
formatter.format_generic_type_args(type_path.turbofish);
formatter.format_generic_type_args(turbofish);
}
}));
group
Expand Down Expand Up @@ -1771,9 +1771,9 @@

#[test]
fn format_method_call_chain_3() {
let src = "fn foo() { assert(p4_affine.eq(Gaffine::new(6890855772600357754907169075114257697580319025794532037257385534741338397365, 4338620300185947561074059802482547481416142213883829469920100239455078257889))); }";

Check warning on line 1774 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
let expected = "fn foo() {
assert(p4_affine.eq(Gaffine::new(

Check warning on line 1776 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)));
Expand Down Expand Up @@ -1809,7 +1809,7 @@
fn format_nested_method_call_with_maximum_width_2() {
let src = "fn foo() {
assert(
p4_affine.eq(Gaffine::new(

Check warning on line 1812 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)),
Expand All @@ -1817,7 +1817,7 @@
}
";
let expected = "fn foo() {
assert(p4_affine.eq(Gaffine::new(

Check warning on line 1820 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)));
Expand Down
Loading