Skip to content

Commit 3eb2a5b

Browse files
committed
Merge branch 'master' into aztec-packages
* master: feat: LSP completion function detail (#5993) feat: add `TypedExpr::get_type` (#5992) feat: better error message for misplaced doc comments (#5990) fix: Error when comptime types are used in runtime code (#5987) fix: suggest trait attributes in LSP (#5972) fix: Error when `quote` is used in runtime code (#5978) chore: document HashMap (#5984) fix: Restrict keccak256_injective test input to 8 bits (#5977) fix: Error when comptime functions are used in runtime code (#5976) chore: document BoundedVec (#5974) feat: add `Expr::as_let` (#5964) chore: remove 3 unused functions warnings in the stdlib (#5973) feat: let `nargo` and LSP work well in the stdlib (#5969)
2 parents ac61ec1 + e84f7d2 commit 3eb2a5b

File tree

26 files changed

+280
-179
lines changed

26 files changed

+280
-179
lines changed

compiler/noirc_frontend/src/elaborator/types.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ impl<'context> Elaborator<'context> {
118118
let fields = self.resolve_type_inner(*fields, kind);
119119
Type::FmtString(Box::new(resolved_size), Box::new(fields))
120120
}
121-
Quoted(quoted) => Type::Quoted(quoted),
121+
Quoted(quoted) => {
122+
let in_function = matches!(self.current_item, Some(DependencyId::Function(_)));
123+
if in_function && !self.in_comptime_context() {
124+
let span = typ.span;
125+
let typ = quoted.to_string();
126+
self.push_err(ResolverError::ComptimeTypeInRuntimeCode { span, typ });
127+
}
128+
Type::Quoted(quoted)
129+
}
122130
Unit => Type::Unit,
123131
Unspecified => {
124132
let span = typ.span;

compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

+26
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ impl<'local, 'context> Interpreter<'local, 'context> {
189189
"typed_expr_as_function_definition" => {
190190
typed_expr_as_function_definition(interner, arguments, return_type, location)
191191
}
192+
"typed_expr_get_type" => {
193+
typed_expr_get_type(interner, arguments, return_type, location)
194+
}
192195
"unresolved_type_is_field" => unresolved_type_is_field(interner, arguments, location),
193196
"zeroed" => zeroed(return_type),
194197
_ => {
@@ -1070,6 +1073,7 @@ fn trait_impl_trait_generic_args(
10701073
Ok(Value::Slice(trait_generics, slice_type))
10711074
}
10721075

1076+
// fn as_function_definition(self) -> Option<FunctionDefinition>
10731077
fn typed_expr_as_function_definition(
10741078
interner: &NodeInterner,
10751079
arguments: Vec<(Value, Location)>,
@@ -1087,6 +1091,28 @@ fn typed_expr_as_function_definition(
10871091
option(return_type, option_value)
10881092
}
10891093

1094+
// fn get_type(self) -> Option<Type>
1095+
fn typed_expr_get_type(
1096+
interner: &NodeInterner,
1097+
arguments: Vec<(Value, Location)>,
1098+
return_type: Type,
1099+
location: Location,
1100+
) -> IResult<Value> {
1101+
let self_argument = check_one_argument(arguments, location)?;
1102+
let typed_expr = get_typed_expr(self_argument)?;
1103+
let option_value = if let TypedExpr::ExprId(expr_id) = typed_expr {
1104+
let typ = interner.id_type(expr_id);
1105+
if typ == Type::Error {
1106+
None
1107+
} else {
1108+
Some(Value::Type(typ))
1109+
}
1110+
} else {
1111+
None
1112+
};
1113+
option(return_type, option_value)
1114+
}
1115+
10901116
// fn is_field(self) -> bool
10911117
fn unresolved_type_is_field(
10921118
interner: &NodeInterner,

compiler/noirc_frontend/src/hir/resolution/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ pub enum ResolverError {
126126
OverflowInType { lhs: u32, op: crate::BinaryTypeOperator, rhs: u32, span: Span },
127127
#[error("`quote` cannot be used in runtime code")]
128128
QuoteInRuntimeCode { span: Span },
129+
#[error("Comptime-only type `{typ}` cannot be used in runtime code")]
130+
ComptimeTypeInRuntimeCode { typ: String, span: Span },
129131
}
130132

131133
impl ResolverError {
@@ -513,6 +515,13 @@ impl<'a> From<&'a ResolverError> for Diagnostic {
513515
*span,
514516
)
515517
},
518+
ResolverError::ComptimeTypeInRuntimeCode { typ, span } => {
519+
Diagnostic::simple_error(
520+
format!("Comptime-only type `{typ}` cannot be used in runtime code"),
521+
"Comptime-only type used here".to_string(),
522+
*span,
523+
)
524+
},
516525
}
517526
}
518527
}

compiler/noirc_frontend/src/monomorphization/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub enum MonomorphizationError {
99
InternalError { message: &'static str, location: Location },
1010
InterpreterError(InterpreterError),
1111
ComptimeFnInRuntimeCode { name: String, location: Location },
12+
ComptimeTypeInRuntimeCode { typ: String, location: Location },
1213
}
1314

1415
impl MonomorphizationError {
@@ -17,6 +18,7 @@ impl MonomorphizationError {
1718
MonomorphizationError::UnknownArrayLength { location, .. }
1819
| MonomorphizationError::InternalError { location, .. }
1920
| MonomorphizationError::ComptimeFnInRuntimeCode { location, .. }
21+
| MonomorphizationError::ComptimeTypeInRuntimeCode { location, .. }
2022
| MonomorphizationError::NoDefaultType { location, .. } => *location,
2123
MonomorphizationError::InterpreterError(error) => error.get_location(),
2224
}
@@ -51,6 +53,11 @@ impl MonomorphizationError {
5153
"Comptime functions must be in a comptime block to be called".into();
5254
return CustomDiagnostic::simple_error(message, secondary, location.span);
5355
}
56+
MonomorphizationError::ComptimeTypeInRuntimeCode { typ, location } => {
57+
let message = format!("Comptime-only type `{typ}` used in runtime code");
58+
let secondary = "Comptime type used here".into();
59+
return CustomDiagnostic::simple_error(message, secondary, location.span);
60+
}
5461
};
5562

5663
let location = self.location();

compiler/noirc_frontend/src/monomorphization/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,10 @@ impl<'interner> Monomorphizer<'interner> {
10561056
let message = "Unexpected Type::Error found during monomorphization";
10571057
return Err(MonomorphizationError::InternalError { message, location });
10581058
}
1059-
HirType::Quoted(_) => unreachable!("Tried to translate Code type into runtime code"),
1059+
HirType::Quoted(typ) => {
1060+
let typ = typ.to_string();
1061+
return Err(MonomorphizationError::ComptimeTypeInRuntimeCode { typ, location });
1062+
}
10601063
})
10611064
}
10621065

compiler/noirc_frontend/src/parser/errors.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::ast::{Expression, IntegerBitSize};
22
use crate::lexer::errors::LexerErrorKind;
33
use crate::lexer::token::Token;
4+
use crate::token::TokenKind;
45
use small_ord_set::SmallOrdSet;
56
use thiserror::Error;
67

@@ -211,8 +212,17 @@ impl<'a> From<&'a ParserError> for Diagnostic {
211212
other => Diagnostic::simple_error(format!("{other}"), String::new(), error.span),
212213
},
213214
None => {
214-
let primary = error.to_string();
215-
Diagnostic::simple_error(primary, String::new(), error.span)
215+
if matches!(
216+
error.found.kind(),
217+
TokenKind::InnerDocComment | TokenKind::OuterDocComment
218+
) {
219+
let primary = "This doc comment doesn't document anything".to_string();
220+
let secondary = "Consider changing it to a regular `//` comment".to_string();
221+
Diagnostic::simple_error(primary, secondary, error.span)
222+
} else {
223+
let primary = error.to_string();
224+
Diagnostic::simple_error(primary, String::new(), error.span)
225+
}
216226
}
217227
}
218228
}

compiler/noirc_frontend/src/tests.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3451,3 +3451,14 @@ fn constrained_reference_to_unconstrained() {
34513451
panic!("Expected an error about passing a constrained reference to unconstrained");
34523452
};
34533453
}
3454+
3455+
#[test]
3456+
fn comptime_type_in_runtime_code() {
3457+
let source = "pub fn foo(_f: FunctionDefinition) {}";
3458+
let errors = get_program_errors(source);
3459+
assert_eq!(errors.len(), 1);
3460+
assert!(matches!(
3461+
errors[0].0,
3462+
CompilationError::ResolverError(ResolverError::ComptimeTypeInRuntimeCode { .. })
3463+
));
3464+
}

docs/docs/noir/standard_library/meta/typed_expr.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ title: TypedExpr
66

77
## Methods
88

9-
### as_function_definition
9+
### get_type
1010

1111
#include_code as_function_definition noir_stdlib/src/meta/typed_expr.nr rust
1212

13-
If this expression refers to a function definitions, returns it. Otherwise returns `Option::none()`.
13+
If this expression refers to a function definitions, returns it. Otherwise returns `Option::none()`.
14+
15+
### get_type
16+
17+
#include_code get_type noir_stdlib/src/meta/typed_expr.nr rust
18+
19+
Returns the type of the expression, or `Option::none()` if there were errors when the expression was previously resolved.

0 commit comments

Comments
 (0)