From 5859baf92d2de01cfd8d451f4af14fce02c73578 Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Mon, 12 Sep 2022 12:31:53 +0800 Subject: [PATCH] Error on unknown reference --- crates/core/src/codegen/mod.rs | 8 ++++++-- crates/core/src/error.rs | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/core/src/codegen/mod.rs b/crates/core/src/codegen/mod.rs index 1b7cb662..0fb1c361 100644 --- a/crates/core/src/codegen/mod.rs +++ b/crates/core/src/codegen/mod.rs @@ -6,6 +6,7 @@ use crate::target::{ DiscriminatorVariantInfo, EnumMember, EnumMemberNamingStrategy, Expr, Field, FilePartitioningStrategy, Item, Strategy, Target, }; +use crate::Error; use ast::{Ast, SchemaAst}; use jtd::Schema; use namespace::Namespace; @@ -120,8 +121,11 @@ impl<'a, T: Target> CodeGenerator<'a, T> { // Ref nodes are a special sort of "expr-like" node, where we // already know what the name of the expression is; it's the name of // the definition. - Ast::Ref { definition, .. } => self.definition_names[&definition].clone(), - + Ast::Ref { definition, .. } => self + .definition_names + .get(&definition) + .ok_or_else(|| Error::UnknownReference(definition.clone()))? + .clone(), // The remaining "expr-like" node types just build up strings and // possibly alter the per-file state (usually in order to add // "imports" to the file). diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index a9bf6ca2..6bd10aa1 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -11,4 +11,7 @@ pub enum Error { #[error("i/o error: {0}")] Io(#[from] io::Error), + + #[error("unknown reference: {0}")] + UnknownReference(String), }