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
8 changes: 4 additions & 4 deletions compiler/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,8 @@ impl<'a> Lexer<'a> {
if self.peek_char_is('=') {
self.next_char();
Ok(Token::LessEqual.into_span(start, start + 1))
} else if self.peek_char_is('<') {
self.next_char();
Ok(Token::ShiftLeft.into_span(start, start + 1))
// Note: There is deliberately no case for ShiftLeft. We always lex << as
// two separate Less tokens to help the parser parse nested generic types.
} else {
Ok(prev_token.into_single_span(start))
}
Expand Down Expand Up @@ -963,7 +962,8 @@ mod tests {
Token::Star,
Token::Assign,
Token::Equal,
Token::ShiftLeft,
Token::Less,
Token::Less,
Token::Greater,
Token::Greater,
Token::EOF,
Expand Down
10 changes: 8 additions & 2 deletions compiler/noirc_frontend/src/parser/parser/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ impl Parser<'_> {
parse_infix!(
self,
Parser::parse_shift,
if self.eat(Token::Less) {
if self.next_token.token() != &Token::LessEqual && self.eat(Token::Less) {
// Make sure to skip the `<<=` case, as `<<=` is lexed as `< <=`.
BinaryOpKind::Less
} else if self.eat(Token::LessEqual) {
BinaryOpKind::LessEqual
Expand All @@ -141,7 +142,12 @@ impl Parser<'_> {
parse_infix!(
self,
Parser::parse_add_or_subtract,
if !self.next_is(Token::Assign) && self.eat(Token::ShiftLeft) {
if self.at(Token::Less) && self.next_is(Token::Less) {
// Left-shift (<<) is issued as two separate < tokens by the lexer as this makes it easier
// to parse nested generic types. For normal expressions however, it means we have to manually
// parse two less-than tokens as a single left-shift here.
self.bump();
self.bump();
BinaryOpKind::ShiftLeft
} else if self.at(Token::Greater) && self.next_is(Token::Greater) {
// Right-shift (>>) is issued as two separate > tokens by the lexer as this makes it easier
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/parser/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,15 @@ impl Parser<'_> {
Token::Percent => Some(BinaryOpKind::Modulo),
Token::Ampersand => Some(BinaryOpKind::And),
Token::Caret => Some(BinaryOpKind::Xor),
Token::ShiftLeft => Some(BinaryOpKind::ShiftLeft),
Token::Pipe => Some(BinaryOpKind::Or),
_ => None,
}
} else if self.at(Token::Greater) && self.next_is(Token::GreaterEqual) {
// >>=
Some(BinaryOpKind::ShiftRight)
} else if self.at(Token::Less) && self.next_is(Token::LessEqual) {
// <<=
Some(BinaryOpKind::ShiftLeft)
} else {
None
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1628,9 +1628,8 @@ fn associated_type_shorthand_in_param_position() {
assert_no_errors(src);
}

/// TODO(https://github.com/noir-lang/noir/issues/11549): remove should_panic once fixed
/// Regression test for https://github.com/noir-lang/noir/issues/11549
#[test]
#[should_panic(expected = "Expected no errors")]
fn nested_associated_type_access_fails() {
// Bug: nested associated type resolution fails
let src = r#"
Expand Down
25 changes: 23 additions & 2 deletions compiler/noirc_frontend/src/tests/traits/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
// `impl T` syntax is expected to be desugared to a `where` clause
fn test_eq(x: impl Eq2) -> bool {
^^^ `impl Trait` as a type is experimental
~~~ Pass -Ztrait_as_type to nargo to enable this feature at your own risk.

Check warning on line 139 in compiler/noirc_frontend/src/tests/traits/trait_bounds.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Ztrait)
x.eq2(x)
}

Expand Down Expand Up @@ -179,9 +179,9 @@
// `impl T` syntax is expected to be desugared to a `where` clause
fn test_eq(x: impl Eq2, y: impl Test) -> bool {
^^^ `impl Trait` as a type is experimental
~~~ Pass -Ztrait_as_type to nargo to enable this feature at your own risk.

Check warning on line 182 in compiler/noirc_frontend/src/tests/traits/trait_bounds.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Ztrait)
^^^^ `impl Trait` as a type is experimental
~~~~ Pass -Ztrait_as_type to nargo to enable this feature at your own risk.

Check warning on line 184 in compiler/noirc_frontend/src/tests/traits/trait_bounds.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Ztrait)
x.eq2(x) == y.test()
}

Expand Down Expand Up @@ -650,6 +650,28 @@
assert_no_errors(src);
}

// Regression test for https://github.com/noir-lang/noir/issues/11553
#[test]
fn nested_angle_brackets_in_type_position() {
let src = r#"
pub trait HasKey {
type Key;
}

pub struct Store<K> {
key: K,
}

pub fn make_store<T>(key: <T as HasKey>::Key) -> Store<<T as HasKey>::Key>
where
T: HasKey,
{
Store { key }
}
"#;
assert_no_errors(src);
}

#[test]
fn where_clause_on_constructed_generic_type() {
let src = r#"
Expand Down Expand Up @@ -1316,9 +1338,8 @@
assert_no_errors(src);
}

/// TODO(https://github.com/noir-lang/noir/issues/11553): remove should_panic once fixed
/// Regression test for https://github.com/noir-lang/noir/issues/11553
#[test]
#[should_panic(expected = "Expected no errors")]
fn associated_type_as_generic_trait_param_with_nested_angle_brackets() {
// Bug: Parser fails on << in type position: Store<<T as HasKey>::Key>
let src = r#"
Expand Down
10 changes: 8 additions & 2 deletions tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,14 @@

group.space_or_line();
group.text(self.chunk(|formatter| {
let tokens_count =
if infix.operator.contents == BinaryOpKind::ShiftRight { 2 } else { 1 };
let tokens_count = if matches!(
infix.operator.contents,
BinaryOpKind::ShiftRight | BinaryOpKind::ShiftLeft
) {
2
} else {
1
};
for _ in 0..tokens_count {
formatter.write_current_token();
formatter.bump();
Expand Down Expand Up @@ -2662,7 +2668,7 @@
fn regression_9556() {
let src = r#"fn foo() {
let x = a()
.bcde(fghijk

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fghijk)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bcde)
);
x
}
Expand Down
Loading