refactor(tvix/eval): use new ToSpan trait wherever possible

... it would be nice if we could thread it through to the `Scope`
stuff (declaring phantoms & locals).

Change-Id: Id3b84e79032b8fbb12138b719e657565355fbc79
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6616
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
Vincent Ambo 2022-09-16 22:01:06 +03:00 committed by tazjin
parent 337d670d0c
commit 1ee0f670b9

View file

@ -200,7 +200,7 @@ impl Compiler<'_, '_> {
} }
ast::LiteralKind::Uri(u) => { ast::LiteralKind::Uri(u) => {
self.emit_warning(self.span_for(&node), WarningKind::DeprecatedLiteralURL); self.emit_warning(&node, WarningKind::DeprecatedLiteralURL);
self.emit_constant(Value::String(u.syntax().text().into()), &node); self.emit_constant(Value::String(u.syntax().text().into()), &node);
} }
} }
@ -218,7 +218,7 @@ impl Compiler<'_, '_> {
Some(buf) => buf, Some(buf) => buf,
None => { None => {
self.emit_error( self.emit_error(
self.span_for(&node), &node,
ErrorKind::PathResolution("failed to determine home directory".into()), ErrorKind::PathResolution("failed to determine home directory".into()),
); );
return; return;
@ -233,9 +233,8 @@ impl Compiler<'_, '_> {
buf buf
} else { } else {
// TODO: decide what to do with findFile // TODO: decide what to do with findFile
let span = self.span_for(&node);
self.emit_error( self.emit_error(
span, &node,
ErrorKind::NotImplemented( ErrorKind::NotImplemented(
"other path types (e.g. <...> lookups) not yet implemented", "other path types (e.g. <...> lookups) not yet implemented",
), ),
@ -532,7 +531,7 @@ impl Compiler<'_, '_> {
// scope is a no-op *if* the identifier can be // scope is a no-op *if* the identifier can be
// statically resolved. // statically resolved.
None if !rec_attrs && !self.scope().has_with() => { None if !rec_attrs && !self.scope().has_with() => {
self.emit_warning(self.span_for(&inherit), WarningKind::UselessInherit); self.emit_warning(&inherit, WarningKind::UselessInherit);
continue; continue;
} }
@ -548,7 +547,7 @@ impl Compiler<'_, '_> {
LocalPosition::Known(_) LocalPosition::Known(_)
) )
{ {
self.emit_warning(self.span_for(&ident), WarningKind::UselessInherit); self.emit_warning(&ident, WarningKind::UselessInherit);
continue; continue;
} }
@ -638,9 +637,8 @@ impl Compiler<'_, '_> {
}; };
if path.len() != 1 { if path.len() != 1 {
let span = self.span_for(&entry);
self.emit_error( self.emit_error(
span, &entry,
ErrorKind::NotImplemented("nested bindings in recursive scope :("), ErrorKind::NotImplemented("nested bindings in recursive scope :("),
); );
continue; continue;
@ -762,7 +760,7 @@ impl Compiler<'_, '_> {
} }
// Otherwise, this variable is missing. // Otherwise, this variable is missing.
self.emit_error(self.span_for(&node), ErrorKind::UnknownStaticVariable); self.emit_error(&node, ErrorKind::UnknownStaticVariable);
} }
LocalPosition::Known(idx) => { LocalPosition::Known(idx) => {
@ -906,7 +904,7 @@ impl Compiler<'_, '_> {
// TODO: strictly check if all keys have been consumed if // TODO: strictly check if all keys have been consumed if
// there is no ellipsis. // there is no ellipsis.
if pattern.ellipsis_token().is_none() { if pattern.ellipsis_token().is_none() {
self.emit_warning(span, WarningKind::NotImplemented("closed formals")); self.emit_warning(&pattern, WarningKind::NotImplemented("closed formals"));
} }
} }
@ -989,14 +987,12 @@ impl Compiler<'_, '_> {
} }
fn compile_legacy_let(&mut self, slot: LocalIdx, node: ast::LegacyLet) { fn compile_legacy_let(&mut self, slot: LocalIdx, node: ast::LegacyLet) {
self.emit_warning(&node, WarningKind::DeprecatedLegacyLet);
self.scope_mut().begin_scope(); self.scope_mut().begin_scope();
self.compile_recursive_scope(slot, true, &node); self.compile_recursive_scope(slot, true, &node);
self.push_op(OpCode::OpAttrs(Count(node.entries().count())), &node); self.push_op(OpCode::OpAttrs(Count(node.entries().count())), &node);
self.emit_constant(Value::String(SmolStr::new_inline("body").into()), &node); self.emit_constant(Value::String(SmolStr::new_inline("body").into()), &node);
self.push_op(OpCode::OpAttrsSelect, &node); self.push_op(OpCode::OpAttrsSelect, &node);
let warning_span = self.span_for(&node);
self.emit_warning(warning_span, WarningKind::DeprecatedLegacyLet);
} }
/// Compile an expression into a runtime thunk which should be /// Compile an expression into a runtime thunk which should be
@ -1126,7 +1122,7 @@ impl Compiler<'_, '_> {
// stack. This is implemented by a separate instruction. // stack. This is implemented by a separate instruction.
let (popcount, unused_spans) = self.scope_mut().end_scope(); let (popcount, unused_spans) = self.scope_mut().end_scope();
for span in unused_spans { for span in &unused_spans {
self.emit_warning(span, WarningKind::UnusedBinding); self.emit_warning(span, WarningKind::UnusedBinding);
} }
@ -1159,19 +1155,13 @@ impl Compiler<'_, '_> {
}; };
if let Some(global_ident) = key { if let Some(global_ident) = key {
self.emit_warning( self.emit_warning(node, WarningKind::ShadowedGlobal(global_ident));
self.span_for(node),
WarningKind::ShadowedGlobal(global_ident),
);
self.scope_mut().poison(global_ident, depth); self.scope_mut().poison(global_ident, depth);
} }
for other in self.scope().locals.iter().rev() { for other in self.scope().locals.iter().rev() {
if other.has_name(&name) && other.depth == depth { if other.has_name(&name) && other.depth == depth {
self.emit_error( self.emit_error(node, ErrorKind::VariableAlreadyDefined(other.span));
self.span_for(node),
ErrorKind::VariableAlreadyDefined(other.span),
);
break; break;
} }
@ -1264,11 +1254,13 @@ impl Compiler<'_, '_> {
self.push_op(OpCode::OpForce, node); self.push_op(OpCode::OpForce, node);
} }
fn emit_warning(&mut self, span: codemap::Span, kind: WarningKind) { fn emit_warning<N: ToSpan>(&mut self, node: &N, kind: WarningKind) {
let span = self.span_for(node);
self.warnings.push(EvalWarning { kind, span }) self.warnings.push(EvalWarning { kind, span })
} }
fn emit_error(&mut self, span: codemap::Span, kind: ErrorKind) { fn emit_error<N: ToSpan>(&mut self, node: &N, kind: ErrorKind) {
let span = self.span_for(node);
self.errors.push(Error { kind, span }) self.errors.push(Error { kind, span })
} }