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: Always parse all tokens from quoted token streams #6064

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 9 additions & 25 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ use crate::{
def_map::ModuleId,
},
hir_def::function::FunctionBody,
lexer::Lexer,
macros_api::{HirExpression, HirLiteral, Ident, ModuleDefId, NodeInterner, Signedness},
node_interner::{DefinitionKind, TraitImplKind},
parser::{self},
parser,
token::{Attribute, SecondaryAttribute, Token},
Kind, QuotedType, ResolvedGeneric, Shared, Type, TypeVariable,
};

use self::builtin_helpers::{eq_item, get_array, get_ctstring, get_str, get_u8, hash_item};
use self::builtin_helpers::{eq_item, get_array, get_ctstring, get_str, get_u8, hash_item, lex};
use super::Interpreter;

pub(crate) mod builtin_helpers;
Expand Down Expand Up @@ -330,18 +329,15 @@ fn struct_def_add_attribute(
let attribute_location = attribute.1;
let attribute = get_str(interner, attribute)?;

let mut tokens = Lexer::lex(&format!("#[{}]", attribute)).0 .0;
if let Some(Token::EOF) = tokens.last().map(|token| token.token()) {
tokens.pop();
}
let mut tokens = lex(&format!("#[{}]", attribute));
if tokens.len() != 1 {
return Err(InterpreterError::InvalidAttribute {
attribute: attribute.to_string(),
location: attribute_location,
});
}

let token = tokens.into_iter().next().unwrap().into_token();
let token = tokens.remove(0);
let Token::Attribute(attribute) = token else {
return Err(InterpreterError::InvalidAttribute {
attribute: attribute.to_string(),
Expand Down Expand Up @@ -374,19 +370,15 @@ fn struct_def_add_generic(
let generic_location = generic.1;
let generic = get_str(interner, generic)?;

let mut tokens = Lexer::lex(&generic).0 .0;
if let Some(Token::EOF) = tokens.last().map(|token| token.token()) {
tokens.pop();
}

let mut tokens = lex(&generic);
if tokens.len() != 1 {
return Err(InterpreterError::GenericNameShouldBeAnIdent {
name: generic,
location: generic_location,
});
}

let Token::Ident(generic_name) = tokens.pop().unwrap().into_token() else {
let Token::Ident(generic_name) = tokens.remove(0) else {
return Err(InterpreterError::GenericNameShouldBeAnIdent {
name: generic,
location: generic_location,
Expand Down Expand Up @@ -2059,12 +2051,7 @@ fn fmtstr_quoted_contents(
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let (string, _) = get_format_string(interner, self_argument)?;
let (tokens, _) = Lexer::lex(&string);
let mut tokens: Vec<_> = tokens.0.into_iter().map(|token| token.into_token()).collect();
if let Some(Token::EOF) = tokens.last() {
tokens.pop();
}

let tokens = lex(&string);
Ok(Value::Quoted(Rc::new(tokens)))
}

Expand All @@ -2083,18 +2070,15 @@ fn function_def_add_attribute(
let attribute_location = attribute.1;
let attribute = get_str(interpreter.elaborator.interner, attribute)?;

let mut tokens = Lexer::lex(&format!("#[{}]", attribute)).0 .0;
if let Some(Token::EOF) = tokens.last().map(|token| token.token()) {
tokens.pop();
}
let mut tokens = lex(&format!("#[{}]", attribute));
if tokens.len() != 1 {
return Err(InterpreterError::InvalidAttribute {
attribute: attribute.to_string(),
location: attribute_location,
});
}

let token = tokens.into_iter().next().unwrap().into_token();
let token = tokens.remove(0);
let Token::Attribute(attribute) = token else {
return Err(InterpreterError::InvalidAttribute {
attribute: attribute.to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{hash::Hasher, rc::Rc};
use acvm::FieldElement;
use noirc_errors::Location;

use crate::lexer::Lexer;
use crate::{
ast::{
BlockExpression, ExpressionKind, Ident, IntegerBitSize, LValue, Pattern, Signedness,
Expand Down Expand Up @@ -392,11 +393,21 @@ pub(super) fn check_function_not_yet_resolved(
}
}

pub(super) fn lex(input: &str) -> Vec<Token> {
let (tokens, _) = Lexer::lex(input);
let mut tokens: Vec<_> = tokens.0.into_iter().map(|token| token.into_token()).collect();
if let Some(Token::EOF) = tokens.last() {
tokens.pop();
}
tokens
}

pub(super) fn parse<T>(
(value, location): (Value, Location),
parser: impl NoirParser<T>,
rule: &'static str,
) -> IResult<T> {
let parser = parser.then_ignore(chumsky::primitive::end());
let tokens = get_quoted((value, location))?;
let quoted = add_token_spans(tokens.clone(), location.span);
parse_tokens(tokens, quoted, location, parser, rule)
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
Value::Expr(ExprValue::Statement(statement))
}

pub(crate) fn lvalue(lvaue: LValue) -> Self {

Check warning on line 104 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
Value::Expr(ExprValue::LValue(lvaue))

Check warning on line 105 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
}

pub(crate) fn pattern(pattern: Pattern) -> Self {
Expand Down Expand Up @@ -554,6 +554,7 @@
parser: impl NoirParser<T>,
location: Location,
) -> IResult<T> {
let parser = parser.then_ignore(chumsky::primitive::end());
match parser.parse(add_token_spans(tokens.clone(), location.span)) {
Ok(expr) => Ok(expr),
Err(mut errors) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "comptime_parse_all_tokens"
type = "bin"
authors = [""]
compiler_version = ">=0.34.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[foo]
fn main() {}

comptime fn foo(_f: FunctionDefinition) -> Quoted {
let t = quote { Field }.as_type();
// We parse 0 or more top level expressions from attribute output
// so for invalid input we previously "successfully" parsed 0 items.
quote { use $t; }
}
Loading