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
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum ExpressionKind {
Tuple(Vec<Expression>),
Lambda(Box<Lambda>),
Parenthesized(Box<Expression>),
Quote(BlockExpression),
Error,
}

Expand Down Expand Up @@ -495,6 +496,7 @@ impl Display for ExpressionKind {
}
Lambda(lambda) => lambda.fmt(f),
Parenthesized(sub_expr) => write!(f, "({sub_expr})"),
Quote(block) => write!(f, "quote {block}"),
Error => write!(f, "Error"),
}
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ impl<'a> Resolver<'a> {
| Type::Constant(_)
| Type::NamedGeneric(_, _)
| Type::TraitAsType(..)
| Type::Code
| Type::Forall(_, _) => (),

Type::Array(length, element_type) => {
Expand Down Expand Up @@ -1620,6 +1621,9 @@ impl<'a> Resolver<'a> {
})
}),
ExpressionKind::Parenthesized(sub_expr) => return self.resolve_expression(*sub_expr),

// The quoted expression isn't resolved since we don't want errors if variables aren't defined
ExpressionKind::Quote(block) => HirExpression::Quote(block),
};

// If these lines are ever changed, make sure to change the early return
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ impl<'interner> TypeChecker<'interner> {

Type::Function(params, Box::new(lambda.return_type), Box::new(env_type))
}
HirExpression::Quote(_) => Type::Code,
};

self.interner.push_expr_type(*expr_id, typ.clone());
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir_def/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum HirExpression {
Tuple(Vec<ExprId>),
Lambda(HirLambda),
Error,
Quote(crate::BlockExpression),
}

impl HirExpression {
Expand Down
22 changes: 19 additions & 3 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ pub enum Type {
/// bind to an integer without special checks to bind it to a non-type.
Constant(u64),

/// The type of quoted code in macros. This is always a comptime-only type
Code,

/// The result of some type error. Remembering type errors as their own type variant lets
/// us avoid issuing repeat type errors for the same item. For example, a lambda with
/// an invalid type would otherwise issue a new error each time it is called
Expand Down Expand Up @@ -144,6 +147,7 @@ impl Type {
| Type::MutableReference(_)
| Type::Forall(_, _)
| Type::Constant(_)
| Type::Code
| Type::Slice(_)
| Type::Error => unreachable!("This type cannot exist as a parameter to main"),
}
Expand Down Expand Up @@ -626,6 +630,7 @@ impl Type {
| Type::Constant(_)
| Type::NamedGeneric(_, _)
| Type::Forall(_, _)
| Type::Code
| Type::TraitAsType(..) => false,

Type::Array(length, elem) => {
Expand Down Expand Up @@ -689,6 +694,7 @@ impl Type {
| Type::Function(_, _, _)
| Type::MutableReference(_)
| Type::Forall(_, _)
| Type::Code
| Type::Slice(_)
| Type::TraitAsType(..) => false,

Expand Down Expand Up @@ -852,6 +858,7 @@ impl std::fmt::Display for Type {
Type::MutableReference(element) => {
write!(f, "&mut {element}")
}
Type::Code => write!(f, "Code"),
}
}
}
Expand Down Expand Up @@ -1529,6 +1536,7 @@ impl Type {
| Type::Constant(_)
| Type::TraitAsType(..)
| Type::Error
| Type::Code
| Type::Unit => self.clone(),
}
}
Expand Down Expand Up @@ -1570,6 +1578,7 @@ impl Type {
| Type::Constant(_)
| Type::TraitAsType(..)
| Type::Error
| Type::Code
| Type::Unit => false,
}
}
Expand Down Expand Up @@ -1621,9 +1630,14 @@ impl Type {

// Expect that this function should only be called on instantiated types
Forall(..) => unreachable!(),
TraitAsType(..) | FieldElement | Integer(_, _) | Bool | Constant(_) | Unit | Error => {
self.clone()
}
TraitAsType(..)
| FieldElement
| Integer(_, _)
| Bool
| Constant(_)
| Unit
| Code
| Error => self.clone(),
}
}

Expand Down Expand Up @@ -1752,6 +1766,7 @@ impl From<&Type> for PrintableType {
Type::MutableReference(typ) => {
PrintableType::MutableReference { typ: Box::new(typ.as_ref().into()) }
}
Type::Code => unreachable!(),
}
}
}
Expand Down Expand Up @@ -1836,6 +1851,7 @@ impl std::fmt::Debug for Type {
Type::MutableReference(element) => {
write!(f, "&mut {element:?}")
}
Type::Code => write!(f, "Code"),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ pub enum Keyword {
Mod,
Mut,
Pub,
Quote,
Return,
ReturnData,
String,
Expand Down Expand Up @@ -710,6 +711,7 @@ impl fmt::Display for Keyword {
Keyword::Mod => write!(f, "mod"),
Keyword::Mut => write!(f, "mut"),
Keyword::Pub => write!(f, "pub"),
Keyword::Quote => write!(f, "quote"),
Keyword::Return => write!(f, "return"),
Keyword::ReturnData => write!(f, "return_data"),
Keyword::String => write!(f, "str"),
Expand Down Expand Up @@ -756,6 +758,7 @@ impl Keyword {
"mod" => Keyword::Mod,
"mut" => Keyword::Mut,
"pub" => Keyword::Pub,
"quote" => Keyword::Quote,
"return" => Keyword::Return,
"return_data" => Keyword::ReturnData,
"str" => Keyword::String,
Expand Down
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ impl<'interner> Monomorphizer<'interner> {
unreachable!("Encountered HirExpression::MethodCall during monomorphization {hir_method_call:?}")
}
HirExpression::Error => unreachable!("Encountered Error node during monomorphization"),
HirExpression::Quote(_) => unreachable!("quote expression remaining in runtime code"),
};

Ok(expr)
Expand Down Expand Up @@ -954,6 +955,7 @@ impl<'interner> Monomorphizer<'interner> {
HirType::Forall(_, _) | HirType::Constant(_) | HirType::Error => {
unreachable!("Unexpected type {} found", typ)
}
HirType::Code => unreachable!("Tried to translate Code type into runtime code"),
}
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,7 @@ enum TypeMethodKey {
Tuple,
Function,
Generic,
Code,
}

fn get_type_method_key(typ: &Type) -> Option<TypeMethodKey> {
Expand All @@ -1691,6 +1692,7 @@ fn get_type_method_key(typ: &Type) -> Option<TypeMethodKey> {
Type::Tuple(_) => Some(Tuple),
Type::Function(_, _, _) => Some(Function),
Type::NamedGeneric(_, _) => Some(Generic),
Type::Code => Some(Code),
Type::MutableReference(element) => get_type_method_key(element),
Type::Alias(alias, _) => get_type_method_key(&alias.borrow().typ),

Expand Down
16 changes: 15 additions & 1 deletion compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,8 @@ where
nothing().boxed()
},
lambdas::lambda(expr_parser.clone()),
block(statement).map(ExpressionKind::Block),
block(statement.clone()).map(ExpressionKind::Block),
quote(statement),
variable(),
literal(),
))
Expand All @@ -1175,6 +1176,19 @@ where
.labelled(ParsingRuleLabel::Atom)
}

fn quote<'a, P>(statement: P) -> impl NoirParser<ExpressionKind> + 'a
where
P: NoirParser<StatementKind> + 'a,
{
keyword(Keyword::Quote).ignore_then(block(statement)).validate(|block, span, emit| {
emit(ParserError::with_reason(
ParserErrorReason::ExperimentalFeature("quoted expressions"),
span,
));
ExpressionKind::Quote(block)
})
}

fn tuple<P>(expr_parser: P) -> impl NoirParser<Expression>
where
P: ExprParser,
Expand Down
18 changes: 12 additions & 6 deletions tooling/nargo_fmt/src/rewrite/expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use noirc_frontend::{token::Token, ArrayLiteral, Expression, ExpressionKind, Literal, UnaryOp};
use noirc_frontend::{
macros_api::Span, token::Token, ArrayLiteral, BlockExpression, Expression, ExpressionKind,
Literal, UnaryOp,
};

use crate::visitor::{
expr::{format_brackets, format_parens, NewlineMode},
Expand All @@ -20,11 +23,7 @@ pub(crate) fn rewrite(
shape: Shape,
) -> String {
match kind {
ExpressionKind::Block(block) => {
let mut visitor = visitor.fork();
visitor.visit_block(block, span);
visitor.finish()
}
ExpressionKind::Block(block) => rewrite_block(visitor, block, span),
ExpressionKind::Prefix(prefix) => {
let op = match prefix.operator {
UnaryOp::Minus => "-",
Expand Down Expand Up @@ -159,6 +158,13 @@ pub(crate) fn rewrite(
visitor.format_if(*if_expr)
}
ExpressionKind::Lambda(_) | ExpressionKind::Variable(_) => visitor.slice(span).to_string(),
ExpressionKind::Quote(block) => format!("quote {}", rewrite_block(visitor, block, span)),
ExpressionKind::Error => unreachable!(),
}
}

fn rewrite_block(visitor: &FmtVisitor, block: BlockExpression, span: Span) -> String {
let mut visitor = visitor.fork();
visitor.visit_block(block, span);
visitor.finish()
}
3 changes: 2 additions & 1 deletion tooling/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ impl AbiType {
| Type::TypeVariable(_, _)
| Type::NamedGeneric(..)
| Type::Forall(..)
| Type::Code
| Type::Slice(_)
| Type::Function(_, _, _) => unreachable!("Type cannot be used in the abi"),
| Type::Function(_, _, _) => unreachable!("{typ} cannot be used in the abi"),
Type::FmtString(_, _) => unreachable!("format strings cannot be used in the abi"),
Type::MutableReference(_) => unreachable!("&mut cannot be used in the abi"),
}
Expand Down