git.delta.rocks / jrsonnet / refs/commits / 349c41065b93

difftreelog

feat lazy values in builtin

Yaroslav Bolyukin2022-04-20parent: #c7fb888.patch.diff
in: master

8 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/builtin/mod.rs
1use crate::function::StaticBuiltin;2use crate::typed::{Any, PositiveF64, VecVal, M1};3use crate::{4	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},5	equals,6	error::{Error::*, Result},7	operator::evaluate_mod_op,8	primitive_equals, push_frame, throw,9	typed::{Either2, Either4},10	with_state, ArrValue, FuncVal, IndexableVal, Val,11};12use crate::{Either, ObjValue};13use format::{format_arr, format_obj};14use jrsonnet_interner::IStr;15use jrsonnet_parser::ExprLocation;16use serde::Deserialize;17use serde_yaml::DeserializingQuirks;18use std::collections::HashMap;19use std::convert::{TryFrom, TryInto};2021pub mod stdlib;22pub use stdlib::*;2324use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};2526pub mod format;27pub mod manifest;28pub mod sort;2930pub fn std_format(str: IStr, vals: Val) -> Result<String> {31	push_frame(32		None,33		|| format!("std.format of {}", str),34		|| {35			Ok(match vals {36				Val::Arr(vals) => format_arr(&str, &vals.evaluated()?)?,37				Val::Obj(obj) => format_obj(&str, &obj)?,38				o => format_arr(&str, &[o])?,39			})40		},41	)42}4344pub fn std_slice(45	indexable: IndexableVal,46	index: Option<usize>,47	end: Option<usize>,48	step: Option<usize>,49) -> Result<Val> {50	let index = index.unwrap_or(0);51	let end = end.unwrap_or_else(|| match &indexable {52		IndexableVal::Str(_) => usize::MAX,53		IndexableVal::Arr(v) => v.len(),54	});55	let step = step.unwrap_or(1);56	match &indexable {57		IndexableVal::Str(s) => Ok(Val::Str(58			(s.chars()59				.skip(index)60				.take(end - index)61				.step_by(step)62				.collect::<String>())63			.into(),64		)),65		IndexableVal::Arr(arr) => Ok(Val::Arr(66			(arr.iter()67				.skip(index)68				.take(end - index)69				.step_by(step)70				.collect::<Result<Vec<Val>>>()?)71			.into(),72		)),73	}74}7576type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;7778thread_local! {79	pub static BUILTINS: BuiltinsType = {80		[81			("length".into(), builtin_length::INST),82			("type".into(), builtin_type::INST),83			("makeArray".into(), builtin_make_array::INST),84			("codepoint".into(), builtin_codepoint::INST),85			("objectFieldsEx".into(), builtin_object_fields_ex::INST),86			("objectHasEx".into(), builtin_object_has_ex::INST),87			("slice".into(), builtin_slice::INST),88			("substr".into(), builtin_substr::INST),89			("primitiveEquals".into(), builtin_primitive_equals::INST),90			("equals".into(), builtin_equals::INST),91			("modulo".into(), builtin_modulo::INST),92			("mod".into(), builtin_mod::INST),93			("floor".into(), builtin_floor::INST),94			("ceil".into(), builtin_ceil::INST),95			("log".into(), builtin_log::INST),96			("pow".into(), builtin_pow::INST),97			("sqrt".into(), builtin_sqrt::INST),98			("sin".into(), builtin_sin::INST),99			("cos".into(), builtin_cos::INST),100			("tan".into(), builtin_tan::INST),101			("asin".into(), builtin_asin::INST),102			("acos".into(), builtin_acos::INST),103			("atan".into(), builtin_atan::INST),104			("exp".into(), builtin_exp::INST),105			("mantissa".into(), builtin_mantissa::INST),106			("exponent".into(), builtin_exponent::INST),107			("extVar".into(), builtin_ext_var::INST),108			("native".into(), builtin_native::INST),109			("filter".into(), builtin_filter::INST),110			("map".into(), builtin_map::INST),111			("flatMap".into(), builtin_flatmap::INST),112			("foldl".into(), builtin_foldl::INST),113			("foldr".into(), builtin_foldr::INST),114			("sort".into(), builtin_sort::INST),115			("format".into(), builtin_format::INST),116			("range".into(), builtin_range::INST),117			("char".into(), builtin_char::INST),118			("encodeUTF8".into(), builtin_encode_utf8::INST),119			("decodeUTF8".into(), builtin_decode_utf8::INST),120			("md5".into(), builtin_md5::INST),121			("base64".into(), builtin_base64::INST),122			("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),123			("base64Decode".into(), builtin_base64_decode::INST),124			("trace".into(), builtin_trace::INST),125			("join".into(), builtin_join::INST),126			("escapeStringJson".into(), builtin_escape_string_json::INST),127			("manifestJsonEx".into(), builtin_manifest_json_ex::INST),128			("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),129			("reverse".into(), builtin_reverse::INST),130			("id".into(), builtin_id::INST),131			("strReplace".into(), builtin_str_replace::INST),132			("splitLimit".into(), builtin_splitlimit::INST),133			("parseJson".into(), builtin_parse_json::INST),134			("parseYaml".into(), builtin_parse_yaml::INST),135			("asciiUpper".into(), builtin_ascii_upper::INST),136			("asciiLower".into(), builtin_ascii_lower::INST),137			("member".into(), builtin_member::INST),138			("count".into(), builtin_count::INST),139		].iter().cloned().collect()140	};141}142143#[jrsonnet_macros::builtin]144fn builtin_length(x: Either![IStr, VecVal, ObjValue, FuncVal]) -> Result<usize> {145	use Either4::*;146	Ok(match x {147		A(x) => x.chars().count(),148		B(x) => x.0.len(),149		C(x) => x150			.fields_visibility()151			.into_iter()152			.filter(|(_k, v)| *v)153			.count(),154		D(f) => f.args_len(),155	})156}157158#[jrsonnet_macros::builtin]159fn builtin_type(x: Any) -> Result<IStr> {160	Ok(x.0.value_type().name().into())161}162163#[jrsonnet_macros::builtin]164fn builtin_make_array(sz: usize, func: FuncVal) -> Result<VecVal> {165	let mut out = Vec::with_capacity(sz);166	for i in 0..sz {167		out.push(func.evaluate_simple(&[i as f64].as_slice())?)168	}169	Ok(VecVal(out))170}171172#[jrsonnet_macros::builtin]173const fn builtin_codepoint(str: char) -> Result<u32> {174	Ok(str as u32)175}176177#[jrsonnet_macros::builtin]178fn builtin_object_fields_ex(obj: ObjValue, inc_hidden: bool) -> Result<VecVal> {179	let out = obj.fields_ex(inc_hidden);180	Ok(VecVal(out.into_iter().map(Val::Str).collect::<Vec<_>>()))181}182183#[jrsonnet_macros::builtin]184fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {185	Ok(obj.has_field_ex(f, inc_hidden))186}187188#[jrsonnet_macros::builtin]189fn builtin_parse_json(s: IStr) -> Result<Any> {190	let value: serde_json::Value = serde_json::from_str(&s)191		.map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;192	Ok(Any(Val::try_from(&value)?))193}194195#[jrsonnet_macros::builtin]196fn builtin_parse_yaml(s: IStr) -> Result<Any> {197	let value = serde_yaml::Deserializer::from_str_with_quirks(198		&s,199		DeserializingQuirks { old_octals: true },200	);201	let mut out = vec![];202	for item in value {203		let value = serde_json::Value::deserialize(item)204			.map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;205		let val = Val::try_from(&value)?;206		out.push(val);207	}208	Ok(Any(if out.is_empty() {209		Val::Null210	} else if out.len() == 1 {211		out.into_iter().next().unwrap()212	} else {213		Val::Arr(out.into())214	}))215}216217#[jrsonnet_macros::builtin]218fn builtin_slice(219	indexable: IndexableVal,220	index: Option<usize>,221	end: Option<usize>,222	step: Option<usize>,223) -> Result<Any> {224	std_slice(indexable, index, end, step).map(Any)225}226227#[jrsonnet_macros::builtin]228fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {229	Ok(str.chars().skip(from as usize).take(len as usize).collect())230}231232#[jrsonnet_macros::builtin]233fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {234	primitive_equals(&a.0, &b.0)235}236237#[jrsonnet_macros::builtin]238fn builtin_equals(a: Any, b: Any) -> Result<bool> {239	equals(&a.0, &b.0)240}241242#[jrsonnet_macros::builtin]243fn builtin_modulo(a: f64, b: f64) -> Result<f64> {244	Ok(a % b)245}246247#[jrsonnet_macros::builtin]248fn builtin_mod(a: Either![f64, IStr], b: Any) -> Result<Any> {249	use Either2::*;250	Ok(Any(evaluate_mod_op(251		&match a {252			A(v) => Val::Num(v),253			B(s) => Val::Str(s),254		},255		&b.0,256	)?))257}258259#[jrsonnet_macros::builtin]260fn builtin_floor(x: f64) -> Result<f64> {261	Ok(x.floor())262}263264#[jrsonnet_macros::builtin]265fn builtin_ceil(x: f64) -> Result<f64> {266	Ok(x.ceil())267}268269#[jrsonnet_macros::builtin]270fn builtin_log(n: f64) -> Result<f64> {271	Ok(n.ln())272}273274#[jrsonnet_macros::builtin]275fn builtin_pow(x: f64, n: f64) -> Result<f64> {276	Ok(x.powf(n))277}278279#[jrsonnet_macros::builtin]280fn builtin_sqrt(x: PositiveF64) -> Result<f64> {281	Ok(x.0.sqrt())282}283284#[jrsonnet_macros::builtin]285fn builtin_sin(x: f64) -> Result<f64> {286	Ok(x.sin())287}288289#[jrsonnet_macros::builtin]290fn builtin_cos(x: f64) -> Result<f64> {291	Ok(x.cos())292}293294#[jrsonnet_macros::builtin]295fn builtin_tan(x: f64) -> Result<f64> {296	Ok(x.tan())297}298299#[jrsonnet_macros::builtin]300fn builtin_asin(x: f64) -> Result<f64> {301	Ok(x.asin())302}303304#[jrsonnet_macros::builtin]305fn builtin_acos(x: f64) -> Result<f64> {306	Ok(x.acos())307}308309#[jrsonnet_macros::builtin]310fn builtin_atan(x: f64) -> Result<f64> {311	Ok(x.atan())312}313314#[jrsonnet_macros::builtin]315fn builtin_exp(x: f64) -> Result<f64> {316	Ok(x.exp())317}318319fn frexp(s: f64) -> (f64, i16) {320	if 0.0 == s {321		(s, 0)322	} else {323		let lg = s.abs().log2();324		let x = (lg - lg.floor() - 1.0).exp2();325		let exp = lg.floor() + 1.0;326		(s.signum() * x, exp as i16)327	}328}329330#[jrsonnet_macros::builtin]331fn builtin_mantissa(x: f64) -> Result<f64> {332	Ok(frexp(x).0)333}334335#[jrsonnet_macros::builtin]336fn builtin_exponent(x: f64) -> Result<i16> {337	Ok(frexp(x).1)338}339340#[jrsonnet_macros::builtin]341fn builtin_ext_var(x: IStr) -> Result<Any> {342	Ok(Any(with_state(|s| s.settings().ext_vars.get(&x).cloned())343		.ok_or(UndefinedExternalVariable(x))?))344}345346#[jrsonnet_macros::builtin]347fn builtin_native(name: IStr) -> Result<FuncVal> {348	Ok(with_state(|s| s.settings().ext_natives.get(&name).cloned())349		.map(|v| FuncVal::Builtin(v.clone()))350		.ok_or(UndefinedExternalFunction(name))?)351}352353#[jrsonnet_macros::builtin]354fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {355	arr.filter(|val| bool::try_from(func.evaluate_simple(&[Any(val.clone())].as_slice())?))356}357358#[jrsonnet_macros::builtin]359fn builtin_map(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {360	arr.map(|val| func.evaluate_simple(&[Any(val)].as_slice()))361}362363#[jrsonnet_macros::builtin]364fn builtin_flatmap(func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {365	match arr {366		IndexableVal::Str(s) => {367			let mut out = String::new();368			for c in s.chars() {369				match func.evaluate_simple(&[c.to_string()].as_slice())? {370					Val::Str(o) => out.push_str(&o),371					_ => throw!(RuntimeError(372						"in std.join all items should be strings".into()373					)),374				};375			}376			Ok(IndexableVal::Str(out.into()))377		}378		IndexableVal::Arr(a) => {379			let mut out = Vec::new();380			for el in a.iter() {381				let el = el?;382				match func.evaluate_simple(&[Any(el)].as_slice())? {383					Val::Arr(o) => {384						for oe in o.iter() {385							out.push(oe?)386						}387					}388					_ => throw!(RuntimeError(389						"in std.join all items should be arrays".into()390					)),391				};392			}393			Ok(IndexableVal::Arr(out.into()))394		}395	}396}397398#[jrsonnet_macros::builtin]399fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {400	let mut acc = init.0;401	for i in arr.iter() {402		acc = func.evaluate_simple(&[Any(acc), Any(i?)].as_slice())?;403	}404	Ok(Any(acc))405}406407#[jrsonnet_macros::builtin]408fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {409	let mut acc = init.0;410	for i in arr.iter().rev() {411		acc = func.evaluate_simple(&[Any(i?), Any(acc)].as_slice())?;412	}413	Ok(Any(acc))414}415416#[jrsonnet_macros::builtin]417#[allow(non_snake_case)]418fn builtin_sort(arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {419	if arr.len() <= 1 {420		return Ok(arr);421	}422	Ok(ArrValue::Eager(sort::sort(423		arr.evaluated()?,424		keyF.as_ref(),425	)?))426}427428#[jrsonnet_macros::builtin]429fn builtin_format(str: IStr, vals: Any) -> Result<String> {430	std_format(str, vals.0)431}432433#[jrsonnet_macros::builtin]434fn builtin_range(from: i32, to: i32) -> Result<VecVal> {435	if to < from {436		return Ok(VecVal(Vec::new()));437	}438	let mut out = Vec::with_capacity((1 + to as usize - from as usize).max(0));439	for i in from as usize..=to as usize {440		out.push(Val::Num(i as f64));441	}442	Ok(VecVal(out))443}444445#[jrsonnet_macros::builtin]446fn builtin_char(n: u32) -> Result<char> {447	Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)448}449450#[jrsonnet_macros::builtin]451fn builtin_encode_utf8(str: IStr) -> Result<VecVal> {452	Ok(VecVal(453		str.bytes()454			.map(|b| Val::Num(b as f64))455			.collect::<Vec<Val>>(),456	))457}458459#[jrsonnet_macros::builtin]460fn builtin_decode_utf8(arr: Vec<u8>) -> Result<String> {461	Ok(String::from_utf8(arr).map_err(|_| RuntimeError("bad utf8".into()))?)462}463464#[jrsonnet_macros::builtin]465fn builtin_md5(str: IStr) -> Result<String> {466	Ok(format!("{:x}", md5::compute(&str.as_bytes())))467}468469#[jrsonnet_macros::builtin]470fn builtin_trace(#[location] loc: Option<&ExprLocation>, str: IStr, rest: Any) -> Result<Any> {471	eprint!("TRACE:");472	if let Some(loc) = loc {473		with_state(|s| {474			let locs = s.map_source_locations(&loc.0, &[loc.1]);475			eprint!(476				" {}:{}",477				loc.0.file_name().unwrap().to_str().unwrap(),478				locs[0].line479			);480		});481	}482	eprintln!(" {}", str);483	Ok(rest) as Result<Any>484}485486#[jrsonnet_macros::builtin]487fn builtin_base64(input: Either![Vec<u8>, IStr]) -> Result<String> {488	use Either2::*;489	Ok(match input {490		A(a) => base64::encode(a),491		B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),492	})493}494495#[jrsonnet_macros::builtin]496fn builtin_base64_decode_bytes(input: IStr) -> Result<Vec<u8>> {497	Ok(base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?)498}499500#[jrsonnet_macros::builtin]501fn builtin_base64_decode(input: IStr) -> Result<String> {502	let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;503	Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)504}505506#[jrsonnet_macros::builtin]507fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {508	Ok(match sep {509		IndexableVal::Arr(joiner_items) => {510			let mut out = Vec::new();511512			let mut first = true;513			for item in arr.iter() {514				let item = item?.clone();515				if let Val::Arr(items) = item {516					if !first {517						out.reserve(joiner_items.len());518						// TODO: extend519						for item in joiner_items.iter() {520							out.push(item?);521						}522					}523					first = false;524					out.reserve(items.len());525					// TODO: extend526					for item in items.iter() {527						out.push(item?);528					}529				} else {530					throw!(RuntimeError(531						"in std.join all items should be arrays".into()532					));533				}534			}535536			IndexableVal::Arr(out.into())537		}538		IndexableVal::Str(sep) => {539			let mut out = String::new();540541			let mut first = true;542			for item in arr.iter() {543				let item = item?.clone();544				if let Val::Str(item) = item {545					if !first {546						out += &sep;547					}548					first = false;549					out += &item;550				} else {551					throw!(RuntimeError(552						"in std.join all items should be strings".into()553					));554				}555			}556557			IndexableVal::Str(out.into())558		}559	})560}561562#[jrsonnet_macros::builtin]563fn builtin_escape_string_json(str_: IStr) -> Result<String> {564	Ok(escape_string_json(&str_))565}566567#[jrsonnet_macros::builtin]568fn builtin_manifest_json_ex(569	value: Any,570	indent: IStr,571	newline: Option<IStr>,572	key_val_sep: Option<IStr>,573) -> Result<String> {574	let newline = newline.as_deref().unwrap_or("\n");575	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");576	manifest_json_ex(577		&value.0,578		&ManifestJsonOptions {579			padding: &indent,580			mtype: ManifestType::Std,581			newline,582			key_val_sep,583		},584	)585}586587#[jrsonnet_macros::builtin]588fn builtin_manifest_yaml_doc(589	value: Any,590	indent_array_in_object: Option<bool>,591	quote_keys: Option<bool>,592) -> Result<String> {593	manifest_yaml_ex(594		&value.0,595		&ManifestYamlOptions {596			padding: "  ",597			arr_element_padding: if indent_array_in_object.unwrap_or(false) {598				"  "599			} else {600				""601			},602			quote_keys: quote_keys.unwrap_or(true),603		},604	)605}606607#[jrsonnet_macros::builtin]608fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {609	Ok(value.reversed())610}611612#[jrsonnet_macros::builtin]613const fn builtin_id(v: Any) -> Result<Any> {614	Ok(v)615}616617#[jrsonnet_macros::builtin]618fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {619	Ok(str.replace(&from as &str, &to as &str))620}621622#[jrsonnet_macros::builtin]623fn builtin_splitlimit(str: IStr, c: char, maxsplits: Either![usize, M1]) -> Result<VecVal> {624	use Either2::*;625	Ok(VecVal(match maxsplits {626		A(n) => str.splitn(n + 1, c).map(|s| Val::Str(s.into())).collect(),627		B(_) => str.split(c).map(|s| Val::Str(s.into())).collect(),628	}))629}630631#[jrsonnet_macros::builtin]632fn builtin_ascii_upper(str: IStr) -> Result<String> {633	Ok(str.to_ascii_uppercase())634}635636#[jrsonnet_macros::builtin]637fn builtin_ascii_lower(str: IStr) -> Result<String> {638	Ok(str.to_ascii_lowercase())639}640641#[jrsonnet_macros::builtin]642fn builtin_member(arr: IndexableVal, x: Any) -> Result<bool> {643	match arr {644		IndexableVal::Str(s) => {645			let x: IStr = IStr::try_from(x.0)?;646			Ok(!x.is_empty() && s.contains(&*x))647		}648		IndexableVal::Arr(a) => {649			for item in a.iter() {650				let item = item?;651				if equals(&item, &x.0)? {652					return Ok(true);653				}654			}655			Ok(false)656		}657	}658}659660#[jrsonnet_macros::builtin]661fn builtin_count(arr: Vec<Any>, v: Any) -> Result<usize> {662	let mut count = 0;663	for item in arr.iter() {664		if equals(&item.0, &v.0)? {665			count += 1;666		}667	}668	Ok(count)669}
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -4,6 +4,7 @@
 	builtin::{std_slice, BUILTINS},
 	error::Error::*,
 	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
+	function::CallLocation,
 	gc::TraceBox,
 	push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
 	FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,
@@ -12,8 +13,8 @@
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{
-	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,
-	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
+	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,
+	LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
 };
 use jrsonnet_types::ValType;
 pub mod operator;
@@ -192,7 +193,7 @@
 	Ok(match field_name {
 		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
 		jrsonnet_parser::FieldName::Dyn(expr) => push_frame(
-			Some(&expr.1),
+			CallLocation::new(&expr.1),
 			|| "evaluating field name".to_string(),
 			|| {
 				let value = evaluate(context, expr)?;
@@ -442,7 +443,7 @@
 	context: Context,
 	value: &LocExpr,
 	args: &ArgsDesc,
-	loc: Option<&ExprLocation>,
+	loc: CallLocation,
 	tailstrict: bool,
 ) -> Result<Val> {
 	let value = evaluate(context.clone(), value)?;
@@ -463,13 +464,13 @@
 	let value = &assertion.0;
 	let msg = &assertion.1;
 	let assertion_result = push_frame(
-		Some(&value.1),
+		CallLocation::new(&value.1),
 		|| "assertion condition".to_owned(),
 		|| bool::try_from(evaluate(context.clone(), value)?),
 	)?;
 	if !assertion_result {
 		push_frame(
-			Some(&value.1),
+			CallLocation::new(&value.1),
 			|| "assertion failure".to_owned(),
 			|| {
 				if let Some(msg) = msg {
@@ -519,7 +520,7 @@
 		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
 		Var(name) => push_frame(
-			Some(loc),
+			CallLocation::new(loc),
 			|| format!("variable <{}> access", name),
 			|| context.binding(name.clone())?.evaluate(),
 		)?,
@@ -528,7 +529,7 @@
 				(Val::Obj(v), Val::Str(s)) => {
 					let sn = s.clone();
 					push_frame(
-						Some(loc),
+						CallLocation::new(loc),
 						|| format!("field <{}> access", sn),
 						|| {
 							if let Some(v) = v.get(s.clone())? {
@@ -625,7 +626,7 @@
 			&Val::Obj(evaluate_object(context, t)?),
 		)?,
 		Apply(value, args, tailstrict) => {
-			evaluate_apply(context, value, args, Some(loc), *tailstrict)?
+			evaluate_apply(context, value, args, CallLocation::new(loc), *tailstrict)?
 		}
 		Function(params, body) => {
 			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
@@ -640,7 +641,7 @@
 			evaluate(context, returned)?
 		}
 		ErrorStmt(e) => push_frame(
-			Some(loc),
+			CallLocation::new(loc),
 			|| "error statement".to_owned(),
 			|| throw!(RuntimeError(IStr::try_from(evaluate(context, e)?)?,)),
 		)?,
@@ -650,7 +651,7 @@
 			cond_else,
 		} => {
 			if push_frame(
-				Some(loc),
+				CallLocation::new(loc),
 				|| "if condition".to_owned(),
 				|| bool::try_from(evaluate(context.clone(), &cond.0)?),
 			)? {
@@ -689,7 +690,7 @@
 			let mut import_location = tmp.to_path_buf();
 			import_location.pop();
 			push_frame(
-				Some(loc),
+				CallLocation::new(loc),
 				|| format!("import {:?}", path),
 				|| with_state(|s| s.import_file(&import_location, path)),
 			)?
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -12,6 +12,19 @@
 use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
 use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
 
+#[derive(Clone, Copy)]
+pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
+impl<'l> CallLocation<'l> {
+	pub fn new(loc: &'l ExprLocation) -> Self {
+		Self(Some(loc))
+	}
+}
+impl CallLocation<'static> {
+	pub fn native() -> Self {
+		Self(None)
+	}
+}
+
 #[derive(Trace)]
 struct EvaluateLazyVal {
 	context: Context,
@@ -383,12 +396,7 @@
 pub trait Builtin: Trace {
 	fn name(&self) -> &str;
 	fn params(&self) -> &[BuiltinParam];
-	fn call(
-		&self,
-		context: Context,
-		loc: Option<&ExprLocation>,
-		args: &dyn ArgsLike,
-	) -> Result<Val>;
+	fn call(&self, context: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val>;
 }
 
 pub trait StaticBuiltin: Builtin + Send + Sync
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -28,7 +28,7 @@
 pub use dynamic::*;
 use error::{Error::*, LocError, Result, StackTraceElement};
 pub use evaluate::*;
-use function::{Builtin, TlaArg};
+use function::{Builtin, CallLocation, TlaArg};
 use gc::{GcHashMap, TraceBox};
 use gcmodule::{Cc, Trace, Weak};
 pub use import::*;
@@ -173,6 +173,7 @@
 	/// Global state is fine here.
 	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)
 }
+
 pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
 	EVAL_STATE.with(|s| {
 		f(s.borrow().as_ref().expect(
@@ -181,7 +182,7 @@
 	})
 }
 pub fn push_frame<T>(
-	e: Option<&ExprLocation>,
+	e: CallLocation,
 	frame_desc: impl FnOnce() -> String,
 	f: impl FnOnce() -> Result<T>,
 ) -> Result<T> {
@@ -345,7 +346,7 @@
 	/// Executes code creating a new stack frame
 	pub fn push<T>(
 		&self,
-		e: Option<&ExprLocation>,
+		e: CallLocation,
 		frame_desc: impl FnOnce() -> String,
 		f: impl FnOnce() -> Result<T>,
 	) -> Result<T> {
@@ -368,7 +369,7 @@
 		}
 		if let Err(mut err) = result {
 			err.trace_mut().0.push(StackTraceElement {
-				location: e.cloned(),
+				location: e.0.cloned(),
 				desc: frame_desc(),
 			});
 			return Err(err);
@@ -512,7 +513,7 @@
 					|| {
 						func.evaluate(
 							self.create_default_context(),
-							None,
+							CallLocation::native(),
 							&self.settings().tla_vars,
 							true,
 						)
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,11 +1,10 @@
 #![allow(clippy::type_complexity)]
 
-use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam};
+use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation};
 use crate::gc::TraceBox;
 use crate::Context;
 use crate::{error::Result, Val};
 use gcmodule::Trace;
-use jrsonnet_parser::ExprLocation;
 use std::path::Path;
 use std::rc::Rc;
 
@@ -32,18 +31,13 @@
 		&self.params
 	}
 
-	fn call(
-		&self,
-		context: Context,
-		loc: Option<&ExprLocation>,
-		args: &dyn ArgsLike,
-	) -> Result<Val> {
+	fn call(&self, context: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
 		let args = parse_builtin_call(context, &self.params, args, true)?;
 		let mut out_args = Vec::with_capacity(self.params.len());
 		for p in self.params.iter() {
 			out_args.push(args[&p.name].evaluate()?);
 		}
-		self.handler.call(loc.map(|l| l.0.clone()), &out_args)
+		self.handler.call(loc.0.map(|l| l.0.clone()), &out_args)
 	}
 }
 
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,5 +1,6 @@
 use std::convert::{TryFrom, TryInto};
 
+use gcmodule::Cc;
 use jrsonnet_interner::IStr;
 pub use jrsonnet_macros::Typed;
 use jrsonnet_types::{ComplexValType, ValType};
@@ -8,7 +9,7 @@
 	error::{Error::*, LocError, Result},
 	throw,
 	typed::CheckType,
-	ArrValue, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
+	ArrValue, FuncDesc, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
 };
 
 pub trait TypedObj: Typed {
@@ -431,6 +432,30 @@
 		Ok(Self::Func(value))
 	}
 }
+
+impl Typed for Cc<FuncDesc> {
+	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
+}
+impl TryFrom<Val> for Cc<FuncDesc> {
+	type Error = LocError;
+
+	fn try_from(value: Val) -> Result<Self, Self::Error> {
+		<Self as Typed>::TYPE.check(&value)?;
+		match value {
+			Val::Func(FuncVal::Normal(desc)) => Ok(desc.clone()),
+			Val::Func(_) => throw!(RuntimeError("expected normal function, not builtin".into())),
+			_ => unreachable!(),
+		}
+	}
+}
+impl TryFrom<Cc<FuncDesc>> for Val {
+	type Error = LocError;
+
+	fn try_from(value: Cc<FuncDesc>) -> Result<Self, Self::Error> {
+		Ok(Self::Func(FuncVal::Normal(value)))
+	}
+}
+
 impl Typed for ObjValue {
 	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);
 }
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -6,14 +6,15 @@
 	error::{Error::*, LocError},
 	evaluate,
 	function::{
-		parse_default_function_call, parse_function_call, ArgsLike, Builtin, StaticBuiltin,
+		parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,
+		StaticBuiltin,
 	},
 	gc::TraceBox,
 	throw, Context, ObjValue, Result,
 };
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{LocExpr, ParamsDesc};
 use jrsonnet_types::ValType;
 use std::{cell::RefCell, fmt::Debug, rc::Rc};
 
@@ -152,7 +153,7 @@
 	pub fn evaluate(
 		&self,
 		call_ctx: Context,
-		loc: Option<&ExprLocation>,
+		loc: CallLocation,
 		args: &dyn ArgsLike,
 		tailstrict: bool,
 	) -> Result<Val> {
@@ -166,7 +167,7 @@
 		}
 	}
 	pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {
-		self.evaluate(Context::default(), None, args, true)
+		self.evaluate(Context::default(), CallLocation::native(), args, true)
 	}
 }
 
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,5 +1,5 @@
 use proc_macro2::TokenStream;
-use quote::{quote, quote_spanned};
+use quote::quote;
 use syn::{
 	parenthesized,
 	parse::{Parse, ParseStream},
@@ -7,8 +7,8 @@
 	punctuated::Punctuated,
 	spanned::Spanned,
 	token::Comma,
-	Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, PatType,
-	Path, PathArguments, Result, Token, Type,
+	Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,
+	PathArguments, Result, ReturnType, Token, Type,
 };
 
 fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>
@@ -33,49 +33,42 @@
 	Ok(Some(attr))
 }
 
-fn is_location_arg(t: &PatType) -> bool {
-	t.attrs.iter().any(|a| a.path.is_ident("location"))
-}
-fn is_self_arg(t: &PatType) -> bool {
-	t.attrs.iter().any(|a| a.path.is_ident("self"))
+fn path_is(path: &Path, needed: &str) -> bool {
+	path.leading_colon.is_none()
+		&& path.segments.len() >= 1
+		&& path.segments.iter().last().unwrap().ident == needed
 }
 
-trait RetainHad<T> {
-	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool;
-}
-impl<T> RetainHad<T> for Vec<T> {
-	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool {
-		let before = self.len();
-		self.retain(h);
-		let after = self.len();
-		before != after
+fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {
+	match ty {
+		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {
+			let args = &path.path.segments.iter().last().unwrap().arguments;
+			Some(args)
+		}
+		_ => None,
 	}
 }
 
-fn extract_type_from_option(ty: &Type) -> Option<&Type> {
-	fn path_is_option(path: &Path) -> bool {
-		path.leading_colon.is_none()
-			&& path.segments.len() == 1
-			&& path.segments.iter().next().unwrap().ident == "Option"
-	}
-
-	match ty {
-		Type::Path(typepath) if typepath.qself.is_none() && path_is_option(&typepath.path) => {
-			// Get the first segment of the path (there is only one, in fact: "Option"):
-			let type_params = &typepath.path.segments.iter().next().unwrap().arguments;
-			// It should have only on angle-bracketed param ("<String>"):
-			let generic_arg = match type_params {
-				PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),
-				_ => panic!("missing option generic"),
-			};
-			// This argument must be a type:
-			match generic_arg {
-				GenericArgument::Type(ty) => Some(ty),
-				_ => panic!("option generic should be a type"),
+fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {
+	Ok(if let Some(args) = type_is_path(ty, "Option") {
+		// It should have only on angle-bracketed param ("<String>"):
+		let generic_arg = match args {
+			PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),
+			_ => return Err(Error::new(args.span(), "missing option generic")),
+		};
+		// This argument must be a type:
+		match generic_arg {
+			GenericArgument::Type(ty) => Some(ty),
+			_ => {
+				return Err(Error::new(
+					generic_arg.span(),
+					"option generic should be a type",
+				))
 			}
 		}
-		_ => None,
-	}
+	} else {
+		None
+	})
 }
 
 struct Field {
@@ -101,7 +94,7 @@
 
 struct EmptyAttr;
 impl Parse for EmptyAttr {
-	fn parse(input: ParseStream) -> Result<Self> {
+	fn parse(_input: ParseStream) -> Result<Self> {
 		Ok(Self)
 	}
 }
@@ -124,107 +117,158 @@
 	}
 }
 
+enum ArgInfo {
+	Normal {
+		ty: Type,
+		is_option: bool,
+		name: String,
+		// ident: Ident,
+	},
+	Lazy {
+		is_option: bool,
+		name: String,
+	},
+	Location,
+	This,
+}
+
+impl ArgInfo {
+	fn parse(arg: &FnArg) -> Result<Self> {
+		let typed = match arg {
+			FnArg::Receiver(_) => unreachable!(),
+			FnArg::Typed(a) => a,
+		};
+		let ident = match &typed.pat as &Pat {
+			Pat::Ident(i) => i.ident.clone(),
+			_ => {
+				return Err(Error::new(
+					typed.pat.span(),
+					"arg should be plain identifier",
+				))
+			}
+		};
+		let ty = &typed.ty as &Type;
+		if type_is_path(&ty, "CallLocation").is_some() {
+			return Ok(Self::Location);
+		} else if type_is_path(&ty, "Self").is_some() {
+			return Ok(Self::This);
+		} else if type_is_path(&ty, "LazyVal").is_some() {
+			return Ok(Self::Lazy {
+				is_option: false,
+				name: ident.to_string(),
+			});
+		}
+
+		let (is_option, ty) = if let Some(ty) = extract_type_from_option(&ty)? {
+			if type_is_path(&ty, "LazyVal").is_some() {
+				return Ok(Self::Lazy {
+					is_option: true,
+					name: ident.to_string(),
+				});
+			}
+
+			(true, ty.clone())
+		} else {
+			(false, ty.clone())
+		};
+
+		Ok(Self::Normal {
+			ty,
+			is_option,
+			name: ident.to_string(),
+			// ident,
+		})
+	}
+}
+
 #[proc_macro_attribute]
 pub fn builtin(
 	attr: proc_macro::TokenStream,
 	item: proc_macro::TokenStream,
 ) -> proc_macro::TokenStream {
-	let attrs = parse_macro_input!(attr as BuiltinAttrs);
-	let mut fun: ItemFn = parse_macro_input!(item);
+	let attr = parse_macro_input!(attr as BuiltinAttrs);
+	let item: ItemFn = parse_macro_input!(item);
+
+	match builtin_inner(attr, item) {
+		Ok(v) => v.into(),
+		Err(e) => e.into_compile_error().into(),
+	}
+}
 
+fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {
 	let result = match fun.sig.output {
-		syn::ReturnType::Default => {
-			return quote_spanned! { fun.sig.span() =>
-				compile_error!("builtins should return something");
-			}
-			.into()
+		ReturnType::Default => {
+			return Err(Error::new(
+				fun.sig.span(),
+				"builtin should return something",
+			))
 		}
-		syn::ReturnType::Type(_, ref ty) => ty.clone(),
+		ReturnType::Type(_, ref ty) => ty.clone(),
 	};
 
-	let params = fun
+	let args = fun
 		.sig
 		.inputs
 		.iter()
-		.map(|i| match i {
-			FnArg::Receiver(_) => unreachable!(),
-			FnArg::Typed(t) => t,
-		})
-		.filter(|a| !is_location_arg(a) && !is_self_arg(a))
-		.map(|t| {
-			let ident = match &t.pat as &Pat {
-				Pat::Ident(i) => i.ident.to_string(),
-				_ => {
-					return quote_spanned! { t.pat.span() =>
-						compile_error!("args should be plain identifiers")
-					}
-					.into()
-				}
-			};
-			let optional = extract_type_from_option(&t.ty).is_some();
-			quote! {
-				BuiltinParam {
-					name: std::borrow::Cow::Borrowed(#ident),
-					has_default: #optional,
-				}
+		.map(|a| ArgInfo::parse(a))
+		.collect::<Result<Vec<_>>>()?;
+
+	let params_desc = args.iter().flat_map(|a| match a {
+		ArgInfo::Normal {
+			is_option, name, ..
+		}
+		| ArgInfo::Lazy { is_option, name } => Some(quote! {
+			BuiltinParam {
+				name: std::borrow::Cow::Borrowed(#name),
+				has_default: #is_option,
 			}
-		})
-		.collect::<Vec<_>>();
+		}),
+		ArgInfo::Location => None,
+		ArgInfo::This => None,
+	});
 
-	let args = fun
-		.sig
-		.inputs
-		.iter_mut()
-		.map(|i| match i {
-			FnArg::Receiver(_) => unreachable!(),
-			FnArg::Typed(t) => t,
-		})
-		.map(|t| {
-			if t.attrs.retain_had(|a| !a.path.is_ident("location")) {
-				quote! {{
-					loc
+	let pass = args.iter().map(|a| match a {
+		ArgInfo::Normal {
+			ty,
+			is_option,
+			name,
+			// ident,
+		} => {
+			let eval = quote! {::jrsonnet_evaluator::push_description_frame(
+				|| format!("argument <{}> evaluation", #name),
+				|| <#ty>::try_from(value.evaluate()?),
+			)?};
+			if *is_option {
+				quote! {if let Some(value) = parsed.get(#name) {
+					Some(#eval)
+				} else {
+					None
 				}}
-			} else if t.attrs.retain_had(|a| !a.path.is_ident("self")) {
+			} else {
 				quote! {{
-					self
+					let value = parsed.get(#name).expect("args shape is checked");
+					#eval
 				}}
-			} else {
-				let ident = match &t.pat as &Pat {
-					Pat::Ident(i) => i.ident.to_string(),
-					_ => {
-						return quote_spanned! { t.pat.span() =>
-							compile_error!("args should be plain identifiers")
-						}
-						.into()
-					}
-				};
-				let ty = &t.ty;
-				if let Some(opt_ty) = extract_type_from_option(&t.ty) {
-					quote! {{
-						if let Some(value) = parsed.get(#ident) {
-							Some(::jrsonnet_evaluator::push_description_frame(
-								|| format!("argument <{}> evaluation", #ident),
-								|| <#opt_ty>::try_from(value.evaluate()?),
-							)?)
-						} else {
-							None
-						}
-					}}
+			}
+		}
+		ArgInfo::Lazy { is_option, name } => {
+			if *is_option {
+				quote! {if let Some(value) = parsed.get(#name) {
+					Some(value.clone())
 				} else {
-					quote! {{
-						let value = parsed.get(#ident).unwrap();
-
-						::jrsonnet_evaluator::push_description_frame(
-							|| format!("argument <{}> evaluation", #ident),
-							|| <#ty>::try_from(value.evaluate()?),
-						)?
-					}}
+					None
+				}}
+			} else {
+				quote! {
+					parsed.get(#name).expect("args shape is correct").clone()
 				}
 			}
-		})
-		.collect::<Vec<_>>();
+		}
+		ArgInfo::Location => quote! {location},
+		ArgInfo::This => quote! {self},
+	});
 
-	let fields = attrs.fields.iter().map(|field| {
+	let fields = attr.fields.iter().map(|field| {
 		let name = &field.name;
 		let ty = &field.ty;
 		quote! {
@@ -234,7 +278,7 @@
 
 	let name = &fun.sig.ident;
 	let vis = &fun.vis;
-	let static_ext = if attrs.fields.is_empty() {
+	let static_ext = if attr.fields.is_empty() {
 		quote! {
 			impl #name {
 				pub const INST: &'static dyn StaticBuiltin = &#name {};
@@ -244,13 +288,13 @@
 	} else {
 		quote! {}
 	};
-	let static_derive_copy = if attrs.fields.is_empty() {
+	let static_derive_copy = if attr.fields.is_empty() {
 		quote! {, Copy}
 	} else {
 		quote! {}
 	};
 
-	(quote! {
+	Ok(quote! {
 		#fun
 		#[doc(hidden)]
 		#[allow(non_camel_case_types)]
@@ -260,12 +304,12 @@
 		}
 		const _: () = {
 			use ::jrsonnet_evaluator::{
-				function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},
+				function::{Builtin, CallLocation, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},
 				error::Result, Context,
 				parser::ExprLocation,
 			};
 			const PARAMS: &'static [BuiltinParam] = &[
-				#(#params),*
+				#(#params_desc),*
 			];
 
 			#static_ext
@@ -279,17 +323,16 @@
 				fn params(&self) -> &[BuiltinParam] {
 					PARAMS
 				}
-				fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
+				fn call(&self, context: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
 					let parsed = parse_builtin_call(context, &PARAMS, args, false)?;
 
-					let result: #result = #name(#(#args),*);
+					let result: #result = #name(#(#pass),*);
 					let result = result?;
 					result.try_into()
 				}
 			}
 		};
 	})
-	.into()
 }
 
 #[derive(Default)]
@@ -366,15 +409,6 @@
 		)
 	}
 
-	fn expand_shallow_field(&self) -> Option<TokenStream> {
-		if self.is_option() {
-			return None;
-		}
-		let name = self.name()?;
-		Some(quote! {
-			(#name, ComplexValType::Any)
-		})
-	}
 	fn expand_field(&self) -> Option<TokenStream> {
 		if self.is_option() {
 			return None;
@@ -450,7 +484,7 @@
 	}
 
 	fn as_option(&self) -> Option<&Type> {
-		extract_type_from_option(&self.0.ty)
+		extract_type_from_option(&self.0.ty).unwrap()
 	}
 	fn is_option(&self) -> bool {
 		self.as_option().is_some()
@@ -460,25 +494,25 @@
 #[proc_macro_derive(Typed, attributes(typed))]
 pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
 	let input = parse_macro_input!(item as DeriveInput);
+
+	match derive_typed_inner(input) {
+		Ok(v) => v.into(),
+		Err(e) => e.to_compile_error().into(),
+	}
+}
+
+fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
 	let data = match &input.data {
 		syn::Data::Struct(s) => s,
-		_ => {
-			return syn::Error::new(input.span(), "only structs supported")
-				.to_compile_error()
-				.into()
-		}
+		_ => return Err(Error::new(input.span(), "only structs supported")),
 	};
 
 	let ident = &input.ident;
-	let fields = match data
+	let fields = data
 		.fields
 		.iter()
 		.map(TypedField::try_new)
-		.collect::<Result<Vec<_>>>()
-	{
-		Ok(v) => v,
-		Err(e) => return e.to_compile_error().into(),
-	};
+		.collect::<Result<Vec<_>>>()?;
 
 	let typed = {
 		let fields = fields
@@ -499,7 +533,7 @@
 	let fields_parse = fields.iter().map(TypedField::expand_parse);
 	let fields_serialize = fields.iter().map(TypedField::expand_serialize);
 
-	quote! {
+	Ok(quote! {
 		const _: () = {
 			use ::jrsonnet_evaluator::{
 				typed::{ComplexValType, Typed, TypedObj, CheckType},
@@ -540,6 +574,5 @@
 			}
 			()
 		};
-	}
-	.into()
+	})
 }