From 746b5037670172029150ba2b907ea8ba8bebbe9d Mon Sep 17 00:00:00 2001 From: Kinflou Date: Mon, 16 Nov 2026 09:28:08 +0800 Subject: [PATCH 1/2] tests: a type name resolves to the declaration the schema imports (#289) compat and the sizer resolve a name by suffix and first match. These cases hold the shape hash and the byte range of a protocol's type to those of the same declaration written in the protocol's own schema: two schemas declaring one name, namespaces that are a string suffix of another, bare, aliased and qualified, and a dependency's names against a consumer's schema of the same name. --- core/tests/schema/ir/mod.rs | 1 + core/tests/schema/ir/type_resolution.rs | 198 ++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 core/tests/schema/ir/type_resolution.rs diff --git a/core/tests/schema/ir/mod.rs b/core/tests/schema/ir/mod.rs index 6ebc9a1..0b6da95 100644 --- a/core/tests/schema/ir/mod.rs +++ b/core/tests/schema/ir/mod.rs @@ -11,3 +11,4 @@ pub mod diff_gaps; pub mod dataless_arrays; pub mod compat_rules; pub mod bounds; +pub mod type_resolution; diff --git a/core/tests/schema/ir/type_resolution.rs b/core/tests/schema/ir/type_resolution.rs new file mode 100644 index 0000000..ee94d26 --- /dev/null +++ b/core/tests/schema/ir/type_resolution.rs @@ -0,0 +1,198 @@ +// A name written in a type is the declaration the schema means, wherever one is +// looked up: `compat` for the shape hashes, the sizer for the byte ranges +// (core#289). Each case names the same declaration a protocol could have +// declared itself, so what a name resolved to shows in a hash and a range that +// the declaration alone would give. + +use comline_core::compat; +use comline_core::package::build::PackageSources; +use comline_core::package::config::ir::context::ProjectContext; +use comline_core::schema::ir::frozen::types::TypeRef; +use comline_core::schema::ir::frozen::unit::FrozenUnit; +use comline_core::schema::ir::size::{self, FrozenLookup}; + +/// The two `User`s a case tells apart: a different size and a different shape. +const BIG: &str = "struct User {\n id: u64\n}\n"; +const SMALL: &str = "struct User {\n id: u8\n}\n"; + +fn users(use_line: &str, reference: &str) -> String { + format!("{use_line}\n\nprotocol Users {{\n function get() -> {reference};\n}}\n") +} + +fn compile(schemas: &[(&[&str], String)]) -> ProjectContext { + let mut sources = PackageSources::new(); + for (namespace, source) in schemas { + sources = sources.schema(namespace.iter().copied(), source.clone()); + } + sources + .compile() + .unwrap_or_else(|e| panic!("should compile: {e}")) +} + +fn shape(context: &ProjectContext, protocol: &str) -> String { + let facts = compat::facts_of_context(context, "0.1.0"); + facts + .protocols + .iter() + .find(|p| p.name == protocol) + .unwrap() + .shape_hash + .clone() +} + +/// What `User` takes under `packed@1`, written in the schema `namespace`. +fn size_of(context: &ProjectContext, namespace: &str, reference: &str) -> Option { + let frozen: Vec> = context + .schema_contexts + .iter() + .filter_map(|s| s.borrow().frozen_schema.borrow().clone()) + .collect(); + let lookup = FrozenLookup::new(frozen.iter().map(Vec::as_slice)); + let scope = lookup.scope_of(namespace).expect("the schema"); + size::range( + &TypeRef::Named(reference.to_string()), + size::rules("packed@1").unwrap(), + &lookup, + scope, + ) +} + +/// The shape and the size of a protocol returning a `User` declared by the +/// protocol's own schema: what any way of reaching that `User` has to give. +fn declared_here(user: &str) -> (String, Option) { + let source = format!("{user}\n{}", users("", "User")); + let context = compile(&[(&["app"], source)]); + (shape(&context, "Users"), size_of(&context, "app", "User")) +} + +/// Schemas that hold the same name under namespaces that resemble each other, +/// in both orders. +fn pairs() -> Vec<(&'static str, [&'static [&'static str]; 2])> { + vec![ + ("v1::api, v2::api", [&["v1", "api"], &["v2", "api"]]), + ("v2::api, v1::api", [&["v2", "api"], &["v1", "api"]]), + ("rapi, api", [&["rapi"], &["api"]]), + ("api, rapi", [&["api"], &["rapi"]]), + ("v1::api, api", [&["v1", "api"], &["api"]]), + ("api, v1::api", [&["api"], &["v1", "api"]]), + ] +} + +#[test] +fn a_name_resolves_to_the_declaration_the_schema_imports() { + let (big, small) = (declared_here(BIG), declared_here(SMALL)); + assert_ne!(big.0, small.0, "the two declarations have different shapes"); + assert_ne!(big.1, small.1, "and different sizes"); + + let mut wrong = vec![]; + for (label, pair) in pairs() { + // The first schema of the pair holds the big `User`, the second the + // small one: each is imported in turn, the other is what a loose + // lookup could take instead. + for imported in 0..2 { + let (path, expected) = ( + pair[imported].join("::"), + if imported == 0 { &big } else { &small }, + ); + let forms = [ + ( + "bare", + format!("use package::{path}::User"), + "User".to_string(), + ), + ( + "aliased", + format!("use package::{path}::User as Account"), + "Account".to_string(), + ), + ( + "qualified", + format!("use package::{path}::User"), + format!("{path}::User"), + ), + ]; + for (form, use_line, reference) in forms { + let context = compile(&[ + (pair[0], BIG.to_string()), + (pair[1], SMALL.to_string()), + (&["app"], users(&use_line, &reference)), + ]); + let shape = shape(&context, "Users"); + if shape != expected.0 { + wrong.push(format!("compat: {label}, imports {path}, {form}")); + } + if size_of(&context, "app", &reference) != expected.1 { + wrong.push(format!("sizer: {label}, imports {path}, {form}")); + } + } + } + } + assert!( + wrong.is_empty(), + "resolved to another declaration than the schema imports:\n{}", + wrong.join("\n") + ); +} + +// ---------- a dependency's names are its own package's ---------- + +#[cfg(feature = "deps")] +fn write(path: &std::path::Path, text: &str) { + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, text).unwrap(); +} + +/// A dependency `p0` whose schema `s1` holds `field` of its sibling `s0`'s +/// `A`, and a package `p1` using that. `p1`'s own `s0` is a schema of the +/// same name, with an `A` of its own when `decoy`. +#[cfg(feature = "deps")] +fn consumer(field: &str, decoy: bool) -> tempfile::TempDir { + let tmp = tempfile::TempDir::new().unwrap(); + let root = tmp.path(); + write( + &root.join("p0/config.idp"), + "congregation p0\nspecification_version = 1\n", + ); + write(&root.join("p0/src/s0.ids"), "struct A {\n f0: u8\n}\n"); + write( + &root.join("p0/src/s1.ids"), + &format!("use package::s0::A\n\nstruct B {{\n f0: {field}\n}}\n"), + ); + write( + &root.join("p1/config.idp"), + "congregation p1\nspecification_version = 1\ndependencies = {\n p0 = {\n path = \"../p0\"\n }\n}\n", + ); + let own = if decoy { + "struct A {\n f0: u64\n}\n\n" + } else { + "" + }; + write( + &root.join("p1/src/s0.ids"), + &format!("use p0::s1::B\n\n{own}protocol Q {{\n function f(a0: B);\n}}\n"), + ); + tmp +} + +#[cfg(feature = "deps")] +fn dependency_shape(field: &str, decoy: bool) -> String { + let tmp = consumer(field, decoy); + let context = + comline_core::package::build::compile_package(&tmp.path().join("p1")).expect("p1 builds"); + shape(&context, "Q") +} + +#[cfg(feature = "deps")] +#[test] +fn a_dependencys_bare_name_is_not_the_consumers_declaration() { + assert_eq!(dependency_shape("A", true), dependency_shape("A", false)); +} + +#[cfg(feature = "deps")] +#[test] +fn a_dependencys_qualified_name_is_not_the_consumers_declaration() { + assert_eq!( + dependency_shape("s0::A", true), + dependency_shape("s0::A", false) + ); +} From bd621ef198090014badc067c7bb62ce473862de0 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Mon, 16 Nov 2026 09:37:20 +0800 Subject: [PATCH 2/2] One resolver for a type name, shared by the validator, compat and the sizer (#289) Resolve a name as the validator accepts it: the schema's own declarations, then its uses (aliases included), then a qualified path equal to a use's. No suffix match and no first declaration of the name. compat and the sizer read a declaration's own names in the schema that declares it, and a dependency's in its own package. --- core/src/compat.rs | 109 ++--- core/src/doc.rs | 5 +- core/src/schema/ir/mod.rs | 1 + core/src/schema/ir/resolve.rs | 457 +++++++++++++++++++++ core/src/schema/ir/size/lookup.rs | 114 ++--- core/src/schema/ir/validation/symbols.rs | 22 + core/src/schema/ir/validation/validator.rs | 19 +- core/tests/schema/ir/type_resolution.rs | 39 +- 8 files changed, 611 insertions(+), 155 deletions(-) create mode 100644 core/src/schema/ir/resolve.rs diff --git a/core/src/compat.rs b/core/src/compat.rs index 9ee911b..f1c6b18 100644 --- a/core/src/compat.rs +++ b/core/src/compat.rs @@ -68,6 +68,7 @@ use crate::schema::ir::diff::{ }; use crate::schema::ir::frozen::types::TypeRef; use crate::schema::ir::frozen::unit::{schema_namespace, ConfigValue, FrozenUnit}; +use crate::schema::ir::resolve::{Found, Resolver, Unresolved}; use crate::schema::ir::unverified::Encodings; // External Uses @@ -1003,59 +1004,64 @@ pub fn encoding(kind: &str, name: &str, params: BTreeMap) -> Enc // ---------- the wire shape ---------- -/// Declarations by qualified and bare name, to resolve what a protocol -/// reaches. +/// The declarations a protocol can reach, resolved as the validator resolved +/// the names that reach them. struct Index<'a> { - qualified: HashMap, - bare: HashMap>, + resolver: Resolver<'a>, /// Errors by ordinal, per namespace. errors: HashMap<(String, u16), &'a FrozenUnit>, } impl<'a> Index<'a> { fn of(loaded: &'a Loaded) -> Self { - let mut index = Index { qualified: HashMap::new(), bare: HashMap::new(), errors: HashMap::new() }; + // The names of the other packages the schemas are of: what a + // dependency's schemas are namespaced under, and `std`. + let packages: BTreeSet<&str> = loaded + .dependencies + .iter() + .flatten() + .filter_map(|units| schema_namespace(units)) + .filter_map(|ns| ns.split("::").next()) + .collect(); + let resolver = Resolver::new(loaded.all().flatten().map(Vec::as_slice), packages); + let mut errors = HashMap::new(); for schemas in loaded.all() { for units in schemas { let ns = schema_namespace(units).unwrap_or(""); for unit in units { - let name = match unit { - FrozenUnit::Struct { name, .. } - | FrozenUnit::Enum { name, .. } - | FrozenUnit::Error { name, .. } - | FrozenUnit::TypeAlias { name, .. } => name, - _ => continue, - }; if let FrozenUnit::Error { ordinal, .. } = unit { - index.errors.insert((ns.to_string(), *ordinal), unit); + errors.insert((ns.to_string(), *ordinal), unit); } - if matches!(unit, FrozenUnit::Error { imported_from: Some(_), .. }) { - continue; - } - index.qualified.insert(format!("{ns}::{name}"), unit); - index.bare.entry(name.clone()).or_default().push((ns, unit)); } } } - index + Index { resolver, errors } + } + + /// The schema whose namespace is `namespace`, as the resolver numbers it. + fn scope_of(&self, namespace: &str) -> usize { + self.resolver + .scope_of(namespace) + .unwrap_or_else(|| panic!("internal error: no schema '{namespace}' among those a protocol reaches")) } - /// The declaration `name` names, written in `namespace`. - fn find(&self, name: &str, namespace: &str) -> Option<&'a FrozenUnit> { - if let Some(unit) = self.qualified.get(name) { - return Some(unit); + /// The declaration `name` names, written in the schema `from`, and the + /// schema it is declared in. `None` for a name in a dependency whose + /// declaration is in a package the dependency has and the version doesn't + /// hold (its own dependency's, which it doesn't publish). + /// + /// Every other name resolved when its schema was validated, so one that + /// doesn't is a bug here, not in the schema. + fn find(&self, name: &str, from: usize) -> Option> { + match self.resolver.resolve(from, name) { + Ok(found) => Some(found), + Err(Unresolved::NotLoaded(_)) if self.resolver.is_other_package(from) => None, + Err(why) => panic!( + "internal error: the name '{name}' written in the schema '{}' names no declaration ({why:?}), \ + though the schema was validated", + self.resolver.namespace(from) + ), } - let last = name.rsplit_once("::").map_or(name, |(_, l)| l); - let candidates = self.bare.get(last)?; - candidates - .iter() - .find(|(ns, _)| *ns == namespace) - .or_else(|| { - let prefix = name.rsplit_once("::").map(|(p, _)| p); - candidates.iter().find(|(ns, _)| prefix.is_some_and(|p| ns.ends_with(p))) - }) - .or(candidates.first()) - .map(|(_, u)| *u) } } @@ -1128,7 +1134,8 @@ struct Builder<'a, 'i> { } impl<'a, 'i> Builder<'a, 'i> { - fn ty(&mut self, ty: &TypeRef, namespace: &str, depth: usize) -> Ty { + /// `ty`, written in the schema `from`. + fn ty(&mut self, ty: &TypeRef, from: usize, depth: usize) -> Ty { if depth > 64 { return Ty::Unresolved(ty.to_string()); } @@ -1137,24 +1144,29 @@ impl<'a, 'i> Builder<'a, 'i> { TypeRef::Unit => Ty::Unit, // A maximum is part of the wire contract (core#98). TypeRef::Array { element, length, max } => { - let element = Box::new(self.ty(element, namespace, depth + 1)); + let element = Box::new(self.ty(element, from, depth + 1)); match max { Some(max) => Ty::Bounded(element, *max), None => Ty::Array(element, *length), } } TypeRef::Union(types) => { - Ty::Union(types.iter().map(|t| self.ty(t, namespace, depth + 1)).collect()) + Ty::Union(types.iter().map(|t| self.ty(t, from, depth + 1)).collect()) } - TypeRef::Named(name) => match self.index.find(name, namespace) { - Some(FrozenUnit::TypeAlias { target, .. }) => self.ty(target, namespace, depth + 1), - Some(unit) => Ty::Declared(self.declare(unit, namespace, depth)), + // A declaration's own names are written in its own schema, which + // is where an alias's target is read too. + TypeRef::Named(name) => match self.index.find(name, from) { + Some(Found { unit: FrozenUnit::TypeAlias { target, .. }, schema }) => { + self.ty(target, schema, depth + 1) + } + Some(Found { unit, schema }) => Ty::Declared(self.declare(unit, schema, depth)), None => Ty::Unresolved(name.rsplit_once("::").map_or(name.as_str(), |(_, l)| l).to_string()), }, } } - fn declare(&mut self, unit: &'a FrozenUnit, namespace: &str, depth: usize) -> u32 { + /// The declaration `unit`, of the schema `from`. + fn declare(&mut self, unit: &'a FrozenUnit, from: usize, depth: usize) -> u32 { let key = unit as *const FrozenUnit; if let Some(i) = self.seen.get(&key) { return *i; @@ -1164,7 +1176,7 @@ impl<'a, 'i> Builder<'a, 'i> { self.declared.push(None); let declared = match unit { FrozenUnit::Struct { fields, parameters, .. } => { - let fields = self.fields(fields, namespace, depth); + let fields = self.fields(fields, from, depth); match max_depth(parameters) { Some(max) => Declared::DepthLimited(fields, max), None => Declared::Struct(fields), @@ -1173,7 +1185,7 @@ impl<'a, 'i> Builder<'a, 'i> { FrozenUnit::Error { fields, ordinal, name, .. } => Declared::Error { ordinal: *ordinal, name: name.clone(), - fields: self.fields(fields, namespace, depth), + fields: self.fields(fields, from, depth), }, FrozenUnit::Enum { variants, .. } => { let mut names: Vec = variants @@ -1194,7 +1206,7 @@ impl<'a, 'i> Builder<'a, 'i> { i } - fn fields(&mut self, fields: &'a [FrozenUnit], namespace: &str, depth: usize) -> Vec { + fn fields(&mut self, fields: &'a [FrozenUnit], from: usize, depth: usize) -> Vec { // Sorted before they're walked, so what they reach is numbered in // an order their declaration order doesn't decide. let mut fields: Vec<&'a FrozenUnit> = fields.iter().collect(); @@ -1209,7 +1221,7 @@ impl<'a, 'i> Builder<'a, 'i> { .filter_map(|f| match f { FrozenUnit::Field { name, ty, optional, .. } => Some(Field { name: name.clone(), - ty: self.ty(ty, namespace, depth + 1), + ty: self.ty(ty, from, depth + 1), optional: *optional, }), _ => None, @@ -1272,6 +1284,7 @@ fn shape_hash_with( let states = |p: Option<&crate::encodings::Profile>, fact: &str, values: &[&str]| { p.and_then(|p| p.facts.get(fact)).is_some_and(|f| values.contains(&f.value.as_str())) }; + let from = index.scope_of(namespace); let mut builder = Builder { index, fields_ordered: !states(format, fact::STRUCT_FIELDS, &["by_name"]), @@ -1292,12 +1305,12 @@ fn shape_hash_with( continue; }; let arguments = - arguments.iter().map(|a| (a.name.clone(), builder.ty(&a.ty, namespace, 0))).collect(); - let returns = _return.as_ref().map(|t| builder.ty(t, namespace, 0)); + arguments.iter().map(|a| (a.name.clone(), builder.ty(&a.ty, from, 0))).collect(); + let returns = _return.as_ref().map(|t| builder.ty(t, from, 0)); let throws = throws .iter() .map(|o| match index.errors.get(&(namespace.to_string(), *o)) { - Some(unit) => Ty::Declared(builder.declare(unit, namespace, 0)), + Some(unit) => Ty::Declared(builder.declare(unit, from, 0)), None => Ty::Unresolved(format!("error #{o}")), }) .collect(); diff --git a/core/src/doc.rs b/core/src/doc.rs index 2a4401b..802824c 100644 --- a/core/src/doc.rs +++ b/core/src/doc.rs @@ -576,7 +576,10 @@ pub fn model(context: &ProjectContext, version: Option) -> DocModel { .collect(); let sizing = Sizing { formats: size_formats(context, &protocols), - lookup: FrozenLookup::new(compiled.iter().map(Vec::as_slice)), + lookup: FrozenLookup::with_packages( + compiled.iter().map(Vec::as_slice), + crate::compat::dependency_names(context.dependency_configs.iter().map(|(name, _)| name.as_str())), + ), }; let mut modules = vec![]; diff --git a/core/src/schema/ir/mod.rs b/core/src/schema/ir/mod.rs index 98ce9a2..dafd521 100644 --- a/core/src/schema/ir/mod.rs +++ b/core/src/schema/ir/mod.rs @@ -10,6 +10,7 @@ pub mod diff; pub mod snippet; #[cfg(feature = "compiler")] pub mod validation; +pub mod resolve; pub mod size; pub mod unverified; pub mod package_wire; diff --git a/core/src/schema/ir/resolve.rs b/core/src/schema/ir/resolve.rs new file mode 100644 index 0000000..fcaebc3 --- /dev/null +++ b/core/src/schema/ir/resolve.rs @@ -0,0 +1,457 @@ +//! What a name written in a type names: one rule for the validator, `compat` +//! and the sizer, so they can't disagree about it (core#289). +//! +//! A name is resolved in the schema it is written in, by what the schema +//! holds and nothing else: +//! +//! 1. a declaration of the schema itself, by its bare name; +//! 2. a `use` of the schema: the name it brings in (its alias, else the last +//! segment of its path), or a qualified path that is its path exactly. +//! +//! There is no suffix match and no taking the first schema that declares the +//! name: two schemas that declare `User` are told apart by which of them the +//! schema imports. A name that nothing in the schema brings in is an +//! [`Unresolved`], which a caller that already validated the schema takes for +//! an internal error. +//! +//! The frozen IR holds what this needs: a schema's units are its declarations +//! and its `Import`s, each import's path resolved to `namespace::Name` (and the +//! namespace of a whole-namespace `use`, a glob's `*`, and every name either +//! brings in, one import each). +//! +//! The validator sees one schema at a time, so it asks only what a `use` of +//! the schema brings in ([`Import::brings`]); [`Resolver`] goes on to the +//! declaration, among the schemas it is given. +//! +//! Qualified references that start with a name a `use` brought into scope +//! (core#290) are this module's to add: `Schema::reach` is where a name is +//! read in its schema, and [`Import::brings`] the rule for what an import is +//! a name for. + +use std::collections::{HashMap, HashSet}; + +use crate::schema::ir::frozen::unit::{schema_namespace, FrozenUnit}; + +/// One `use` of a schema, as the IR freezes it: the path it resolved to and +/// the alias it was given. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Import<'a> { + pub path: &'a str, + pub alias: Option<&'a str>, +} + +impl<'a> Import<'a> { + /// The import `unit` is, if it is one. + pub fn of(unit: &'a FrozenUnit) -> Option { + match unit { + FrozenUnit::Import(path, alias, _) => Some(Import { + path, + alias: alias.as_deref(), + }), + _ => None, + } + } + + /// The name it brings into scope on its own, Rust-style: its alias, else + /// the last segment of its path. A path of one segment (a whole + /// namespace) has none. + pub fn bare(&self) -> Option<&'a str> { + match self.alias { + Some(alias) => Some(alias), + None => self + .path + .rsplit("::") + .next() + .filter(|last| *last != self.path), + } + } + + /// Whether a name written in the schema is this import's: the name it + /// brings in, or its path written out. + pub fn brings(&self, name: &str) -> bool { + self.bare() == Some(name) || self.path == name + } +} + +/// The name a declaration of a schema holds in its symbol table: any kind of +/// declaration, a re-export slot of another schema's error not being one. +pub fn declared_name(unit: &FrozenUnit) -> Option<&str> { + match unit { + FrozenUnit::Struct { name, .. } + | FrozenUnit::Enum { name, .. } + | FrozenUnit::Protocol { name, .. } + | FrozenUnit::Constant { name, .. } + | FrozenUnit::Validator { name, .. } + | FrozenUnit::TypeAlias { name, .. } + | FrozenUnit::Error { + name, + imported_from: None, + .. + } => Some(name), + _ => None, + } +} + +/// Whether a declaration can stand where a type is written. +fn is_type(unit: &FrozenUnit) -> bool { + matches!( + unit, + FrozenUnit::Struct { .. } + | FrozenUnit::Enum { .. } + | FrozenUnit::TypeAlias { .. } + | FrozenUnit::Error { .. } + ) +} + +/// A declaration a name resolved to, and the schema it is declared in: where +/// the names in it are written. +#[derive(Debug, Clone, Copy)] +pub struct Found<'a> { + /// The schema, as [`Resolver::scope_of`] numbers them. + pub schema: usize, + pub unit: &'a FrozenUnit, +} + +/// Why a name resolved to no declaration. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Unresolved<'a> { + /// Nothing the schema declares and no `use` of it brings the name in. + NotInScope, + /// A `use` brings it in, from a schema that wasn't given: a dependency's + /// own dependency, which what a dependency publishes doesn't hold. + NotLoaded(&'a str), + /// It is declared (or brought in), but not a type: a protocol, a + /// constant, a validator, a namespace. + NotAType, + /// A `use` brings it in from a schema that declares nothing of the name. + NotDeclared(&'a str), +} + +/// The schemas a name can be resolved among, each as its units: the +/// declarations and the `use`s of each, and where each begins. +pub struct Resolver<'a> { + schemas: Vec>, + by_namespace: HashMap<&'a str, usize>, + roots: HashSet, +} + +/// One schema, as far as a name in it goes. +struct Schema<'a> { + namespace: &'a str, + /// The package whose schema this is, where it isn't the one being + /// compiled: its `use`s name the schemas of its own package without it. + package: Option<&'a str>, + imports: Vec>, + declared: HashMap<&'a str, &'a FrozenUnit>, +} + +impl<'a> Schema<'a> { + /// What the name `name`, written in this schema, points at on its own: + /// one of its declarations, or the path a `use` of it brings in. `None` + /// where neither does. + fn reach(&self, name: &str) -> Option> { + if let Some(unit) = self.declared.get(name) { + return Some(Reach::Declared(unit)); + } + let imports: Vec<&Import<'a>> = self.imports.iter().filter(|i| i.brings(name)).collect(); + (!imports.is_empty()).then_some(Reach::Imported(imports)) + } +} + +/// What a name points at, before the declaration is found. +enum Reach<'a, 's> { + Declared(&'a FrozenUnit), + /// Each `use` that brings it in, in order. + Imported(Vec<&'s Import<'a>>), +} + +impl<'a> Resolver<'a> { + /// The resolver among `schemas` (each starting with its `Namespace` unit, + /// as the frozen IR keeps them). `packages` are the names the schemas of + /// other packages begin with (a dependency's, `std`): what the `use`s of + /// one of those schemas mean is in its own package. + pub fn new( + schemas: impl IntoIterator, + packages: impl IntoIterator>, + ) -> Self { + let roots: HashSet = packages.into_iter().map(Into::into).collect(); + let schemas: Vec> = schemas + .into_iter() + .map(|units| { + let namespace = schema_namespace(units).unwrap_or(""); + let package = namespace + .split("::") + .next() + .filter(|first| roots.contains(*first)); + let mut declared = HashMap::new(); + for unit in units { + if let Some(name) = declared_name(unit) { + declared.entry(name).or_insert(unit); + } + } + Schema { + namespace, + package, + imports: units.iter().filter_map(Import::of).collect(), + declared, + } + }) + .collect(); + let mut by_namespace = HashMap::new(); + for (at, schema) in schemas.iter().enumerate() { + by_namespace.entry(schema.namespace).or_insert(at); + } + Resolver { + schemas, + by_namespace, + roots, + } + } + + /// The schema whose namespace is `namespace` (`api`, `billing::api`). + pub fn scope_of(&self, namespace: &str) -> Option { + self.by_namespace.get(namespace).copied() + } + + /// The namespace of the schema `at`. + pub fn namespace(&self, at: usize) -> &'a str { + self.schemas[at].namespace + } + + /// Whether the schema `at` is another package's, a dependency's or + /// `std`'s. + pub fn is_other_package(&self, at: usize) -> bool { + self.schemas[at].package.is_some() + } + + /// The declaration `name` names, written in the schema `from`: the one + /// the validator accepted it for. + pub fn resolve(&self, from: usize, name: &str) -> Result, Unresolved<'a>> { + let schema = &self.schemas[from]; + match schema.reach(name).ok_or(Unresolved::NotInScope)? { + Reach::Declared(unit) if is_type(unit) => Ok(Found { schema: from, unit }), + Reach::Declared(_) => Err(Unresolved::NotAType), + // The first of the `use`s that brings the name in and leads to a + // declaration; where none does, why the first didn't. + Reach::Imported(imports) => { + let mut first = None; + for import in imports { + match self.locate(schema, import.path) { + Ok(found) => return Ok(found), + Err(why) => { + first.get_or_insert(why); + } + } + } + Err(first.unwrap_or(Unresolved::NotInScope)) + } + } + } + + /// The declaration the path `namespace::Name` of a `use` in `schema` + /// names. + fn locate(&self, schema: &Schema<'a>, path: &'a str) -> Result, Unresolved<'a>> { + let Some((namespace, name)) = path.rsplit_once("::") else { + return Err(Unresolved::NotAType); + }; + let at = match schema.package { + // A path in another package is written as that package does: + // its own schemas' first, then std and the other packages'. + Some(package) => { + let within = format!("{package}::{namespace}"); + match self.scope_of(&within) { + Some(at) => Some(at), + None if self + .roots + .contains(namespace.split("::").next().unwrap_or(namespace)) => + { + self.scope_of(namespace) + } + None => None, + } + } + None => self.scope_of(namespace), + }; + let at = at.ok_or(Unresolved::NotLoaded(path))?; + match self.schemas[at].declared.get(name) { + Some(unit) if is_type(unit) => Ok(Found { schema: at, unit }), + Some(_) => Err(Unresolved::NotAType), + None => Err(Unresolved::NotDeclared(path)), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn unit(name: &str) -> FrozenUnit { + FrozenUnit::Struct { + docstring: None, + parameters: vec![], + name: name.to_string(), + fields: vec![], + span: (0, 0), + } + } + + fn import(path: &str, alias: Option<&str>) -> FrozenUnit { + FrozenUnit::Import(path.to_string(), alias.map(str::to_string), (0, 0)) + } + + fn schema(namespace: &str, mut units: Vec) -> Vec { + units.insert(0, FrozenUnit::Namespace(namespace.to_string())); + units + } + + /// The namespace of the schema a name resolves to, or why it doesn't. + fn namespace_of<'a>( + resolver: &Resolver<'a>, + schemas: &'a [Vec], + from: &str, + name: &str, + ) -> Result<&'a str, Unresolved<'a>> { + let from = resolver.scope_of(from).unwrap(); + let found = resolver.resolve(from, name)?; + Ok(schema_namespace(&schemas[found.schema]).unwrap()) + } + + #[test] + fn a_name_is_what_the_schema_declares_or_imports() { + let schemas = vec![ + schema("v1::api", vec![unit("User")]), + schema("v2::api", vec![unit("User")]), + schema( + "app", + vec![ + import("v2::api::User", None), + import("v1::api::User", Some("Old")), + unit("Own"), + ], + ), + ]; + let resolver = Resolver::new(schemas.iter().map(Vec::as_slice), Vec::::new()); + let at = |name| namespace_of(&resolver, &schemas, "app", name); + assert_eq!(at("User"), Ok("v2::api"), "bare"); + assert_eq!(at("Old"), Ok("v1::api"), "aliased"); + assert_eq!(at("v2::api::User"), Ok("v2::api"), "qualified"); + assert_eq!( + at("v1::api::User"), + Ok("v1::api"), + "qualified, the path of an aliased import" + ); + assert_eq!(at("Own"), Ok("app"), "its own"); + } + + #[test] + fn nothing_is_matched_by_suffix_or_taken_first() { + let schemas = vec![ + schema("rapi", vec![unit("User")]), + schema("v1::api", vec![unit("User")]), + schema( + "app", + vec![import("api::User", None), import("v1::other::Item", None)], + ), + ]; + let resolver = Resolver::new(schemas.iter().map(Vec::as_slice), Vec::::new()); + let at = |name| namespace_of(&resolver, &schemas, "app", name); + // `api::User` is a path no schema has: not `rapi`'s, not `v1::api`'s. + assert_eq!(at("User"), Err(Unresolved::NotLoaded("api::User"))); + assert_eq!(at("api::User"), Err(Unresolved::NotLoaded("api::User"))); + // Not through the schema's imports: not in scope, whatever declares it. + assert_eq!(at("v1::api::User"), Err(Unresolved::NotInScope)); + assert_eq!(at("Other"), Err(Unresolved::NotInScope)); + assert_eq!(at("api"), Err(Unresolved::NotInScope)); + } + + #[test] + fn a_declaration_shadows_an_import_of_its_name() { + let schemas = vec![ + schema("other", vec![unit("User")]), + schema("app", vec![import("other::User", None), unit("User")]), + ]; + let resolver = Resolver::new(schemas.iter().map(Vec::as_slice), Vec::::new()); + assert_eq!(namespace_of(&resolver, &schemas, "app", "User"), Ok("app")); + } + + #[test] + fn what_is_no_type_is_said_so() { + let constant = FrozenUnit::Constant { + docstring: None, + name: "LIMIT".to_string(), + ty: crate::schema::ir::frozen::types::TypeRef::Unit, + value: None, + span: (0, 0), + }; + let schemas = vec![ + schema("limits", vec![constant]), + schema( + "app", + vec![ + import("limits::LIMIT", None), + import("limits", None), + import("limits::Gone", None), + ], + ), + ]; + let resolver = Resolver::new(schemas.iter().map(Vec::as_slice), Vec::::new()); + let at = |name| namespace_of(&resolver, &schemas, "app", name); + assert_eq!(at("LIMIT"), Err(Unresolved::NotAType)); + assert_eq!(at("Gone"), Err(Unresolved::NotDeclared("limits::Gone"))); + assert_eq!(at("limits"), Err(Unresolved::NotAType)); + } + + #[test] + fn a_dependencys_paths_are_its_packages() { + let schemas = vec![ + // The consumer's own `s0`, with an `A` of its own. + schema("s0", vec![unit("A")]), + schema("p0::s0", vec![unit("A")]), + schema( + "p0::s1", + vec![ + import("s0::A", None), + import("std::http::Request", None), + import("base::Gone", None), + ], + ), + schema("std::http", vec![unit("Request")]), + ]; + let resolver = Resolver::new(schemas.iter().map(Vec::as_slice), ["p0", "std"]); + let at = |name| namespace_of(&resolver, &schemas, "p0::s1", name); + assert_eq!( + at("A"), + Ok("p0::s0"), + "its sibling, not the consumer's schema of the name" + ); + assert_eq!(at("s0::A"), Ok("p0::s0")); + assert_eq!(at("Request"), Ok("std::http"), "std, as written"); + // A package of its own that the consumer doesn't hold isn't guessed at. + assert_eq!(at("Gone"), Err(Unresolved::NotLoaded("base::Gone"))); + assert!(resolver.is_other_package(resolver.scope_of("p0::s1").unwrap())); + assert!(!resolver.is_other_package(resolver.scope_of("s0").unwrap())); + } + + #[test] + fn what_an_import_brings_in() { + let plain = Import { + path: "a::b::Name", + alias: None, + }; + assert_eq!(plain.bare(), Some("Name")); + assert!(plain.brings("Name") && plain.brings("a::b::Name")); + assert!(!plain.brings("b::Name") && !plain.brings("Other")); + let aliased = Import { + path: "a::b::Name", + alias: Some("X"), + }; + assert!(aliased.brings("X") && aliased.brings("a::b::Name") && !aliased.brings("Name")); + // A whole namespace has no bare name, and is no more than its path. + let namespace = Import { + path: "types", + alias: None, + }; + assert_eq!(namespace.bare(), None); + assert!(namespace.brings("types")); + } +} diff --git a/core/src/schema/ir/size/lookup.rs b/core/src/schema/ir/size/lookup.rs index dc760c9..f0970b5 100644 --- a/core/src/schema/ir/size/lookup.rs +++ b/core/src/schema/ir/size/lookup.rs @@ -2,10 +2,10 @@ //! types it holds written as the IR freezes them. use std::borrow::Cow; -use std::collections::HashMap; use crate::schema::ir::frozen::types::TypeRef; -use crate::schema::ir::frozen::unit::{schema_namespace, ConfigValue, FrozenUnit}; +use crate::schema::ir::frozen::unit::{ConfigValue, FrozenUnit}; +use crate::schema::ir::resolve::Resolver; /// A declaration, as far as its size goes. #[derive(Debug, Clone, PartialEq)] @@ -55,93 +55,36 @@ pub trait Lookup { fn resolve(&self, scope: Self::Scope, name: &str) -> Option>; } -/// A [`Lookup`] over frozen schemas: the units of each, resolving a name -/// through the schema's own declarations, its `use`s, a qualified path, and -/// last any schema's declaration of that name, as `compat` finds the -/// declarations a protocol reaches. Its scope is the index of the schema, from +/// A [`Lookup`] over frozen schemas: the units of each, resolving a name as the +/// validator accepted it, through [`Resolver`]: the schema's own declarations, +/// its `use`s, aliases included, and a qualified path that is one of their +/// paths, and nothing looser. Its scope is the index of the schema, from /// [`FrozenLookup::scope_of`]. pub struct FrozenLookup<'a> { - schemas: Vec<(&'a str, &'a [FrozenUnit])>, - /// Each declared name's schemas, in order. - by_name: HashMap<&'a str, Vec>, + resolver: Resolver<'a>, } impl<'a> FrozenLookup<'a> { /// The schemas given (each starting with its `Namespace` unit, as the - /// frozen IR keeps them). + /// frozen IR keeps them), all of the package being sized. pub fn new(schemas: impl IntoIterator) -> Self { - let schemas: Vec<(&str, &[FrozenUnit])> = - schemas.into_iter().map(|units| (schema_namespace(units).unwrap_or(""), units)).collect(); - let mut by_name: HashMap<&str, Vec> = HashMap::new(); - for (at, &(_, units)) in schemas.iter().enumerate() { - for unit in units { - if let Some(name) = declared_name(unit) { - by_name.entry(name).or_default().push(at); - } - } - } - FrozenLookup { schemas, by_name } + Self::with_packages(schemas, Vec::::new()) + } + + /// The schemas given, among them those of other packages: a dependency's + /// or `std`'s, whose namespaces begin with its name, one of `packages`. + /// What a schema of those uses is read in its own package. + pub fn with_packages( + schemas: impl IntoIterator, + packages: impl IntoIterator>, + ) -> Self { + FrozenLookup { resolver: Resolver::new(schemas, packages) } } /// The scope of the schema whose namespace is `namespace` (`api`, /// `billing::api`). pub fn scope_of(&self, namespace: &str) -> Option { - self.schemas.iter().position(|(ns, _)| *ns == namespace) - } - - /// The declaration `name` in the schema `at`. - fn declared(&self, at: usize, name: &str) -> Option<(usize, &'a FrozenUnit)> { - let &(_, units) = self.schemas.get(at)?; - units.iter().find(|u| declared_name(u) == Some(name)).map(|u| (at, u)) - } - - fn find(&self, from: usize, name: &str) -> Option<(usize, &'a FrozenUnit)> { - let (path, last) = match name.rsplit_once("::") { - Some((path, last)) => (Some(path), last), - None => (None, name), - }; - match path { - Some(path) => { - let in_path = |namespace: &str| { - namespace == path - || namespace.ends_with(&format!("::{path}")) - || path.ends_with(&format!("::{namespace}")) - }; - let found = (0..self.schemas.len()) - .filter(|at| in_path(self.schemas[*at].0)) - .find_map(|at| self.declared(at, last)); - if found.is_some() { - return found; - } - } - None => { - if let Some(found) = self.declared(from, last) { - return Some(found); - } - if let Some(found) = self.imported(from, last) { - return Some(found); - } - } - } - // As `compat` finds a name: any schema's declaration of it. - let at = *self.by_name.get(last)?.first()?; - self.declared(at, last) - } - - /// What the schema `from`'s `use`s bring in as `name`. - fn imported(&self, from: usize, name: &str) -> Option<(usize, &'a FrozenUnit)> { - let &(_, units) = self.schemas.get(from)?; - units.iter().find_map(|unit| { - let FrozenUnit::Import(path, alias, _) = unit else { return None }; - if let Some(glob) = path.strip_suffix("::*") { - return self.find(from, &format!("{glob}::{name}")); - } - let (_, last) = path.rsplit_once("::").unwrap_or(("", path)); - if alias.as_deref().unwrap_or(last) != name { - return None; - } - self.find(from, path) - }) + self.resolver.scope_of(namespace) } } @@ -149,19 +92,10 @@ impl Lookup for FrozenLookup<'_> { type Scope = usize; fn resolve(&self, scope: usize, name: &str) -> Option> { - let (at, unit) = self.find(scope, name)?; - Some(Resolved { key: unit as *const FrozenUnit as usize, scope: at, decl: decl(unit) }) - } -} - -fn declared_name(unit: &FrozenUnit) -> Option<&str> { - match unit { - FrozenUnit::Struct { name, .. } | FrozenUnit::Enum { name, .. } | FrozenUnit::TypeAlias { name, .. } => { - Some(name) - } - // An error has no size of its own: a `throws` isn't sized, and one - // named as a field's type is refused by validation. - _ => None, + // A name that doesn't resolve can't be sized: a type written in an + // editor, or in a dependency's own dependency, which isn't here. + let found = self.resolver.resolve(scope, name).ok()?; + Some(Resolved { key: found.unit as *const FrozenUnit as usize, scope: found.schema, decl: decl(found.unit) }) } } diff --git a/core/src/schema/ir/validation/symbols.rs b/core/src/schema/ir/validation/symbols.rs index 13bea0e..3adaea2 100644 --- a/core/src/schema/ir/validation/symbols.rs +++ b/core/src/schema/ir/validation/symbols.rs @@ -1,6 +1,8 @@ // use crate::schema::ir::frozen::unit::FrozenUnit; use std::collections::{HashMap, HashSet}; +use crate::schema::ir::resolve::Import; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SymbolType { Struct, @@ -88,6 +90,10 @@ pub struct SymbolTable<'a> { /// `symbols` so it never causes a "duplicate definition" and a real local /// declaration always shadows it. bare_imports: HashSet<&'a str>, + /// The `use`s themselves, which decide what a name written in a type may + /// be besides a declaration (see [`Import::brings`]): the rule `compat` + /// and the sizer resolve a name by too. + imports: Vec>, } impl<'a> SymbolTable<'a> { @@ -95,7 +101,23 @@ impl<'a> SymbolTable<'a> { Self { symbols: HashMap::new(), bare_imports: HashSet::new(), + imports: Vec::new(), + } + } + + /// Record a `use`: the name it brings into scope bare, if it has one, and + /// the path it can be written out as. + pub fn add_import(&mut self, import: Import<'a>) { + if let Some(bare) = import.bare() { + self.add_bare_import(bare); } + self.imports.push(import); + } + + /// Whether a `use` brings `name` into scope: as the name it binds bare, or + /// as its own path. + pub fn is_imported(&self, name: &str) -> bool { + self.imports.iter().any(|import| import.brings(name)) } /// Record a bare name made available by a `use`. diff --git a/core/src/schema/ir/validation/validator.rs b/core/src/schema/ir/validation/validator.rs index 76b0ffb..b26d41b 100644 --- a/core/src/schema/ir/validation/validator.rs +++ b/core/src/schema/ir/validation/validator.rs @@ -7,6 +7,7 @@ use super::symbols::{SymbolTable, SymbolType}; use crate::schema::ir::frozen::unit::{schema_namespace, ConfigValue, DeprecatedValue, FrozenUnit}; use crate::schema::ir::compiler::interpreter::incremental::{NOT_AN_ERROR_PREFIX, UNRESOLVED_PREFIX}; use crate::schema::ir::frozen::types::{Literal, PrimitiveType, TypeRef}; +use crate::schema::ir::resolve::Import; use super::constants::{Constants, Problem, Resolution, ResolvedConstant}; use crate::utils::text::{closest, levenshtein_distance}; use crate::config_value::{self, ValueType}; @@ -207,14 +208,10 @@ pub fn validate_with( // resolves), and the bare name a `use` brought into scope goes in a // separate set (so it never conflicts and a local declaration shadows // it). Redundant identical `use`s are fine, hence `let _`. - if let FrozenUnit::Import(path, alias, _span) = unit { - let _ = symbols.insert(path.as_str(), SymbolType::Import, Some(*_span)); - let bare = match alias { - Some(a) => Some(a.as_str()), - None => path.rsplit("::").next().filter(|s| *s != path.as_str()), - }; - if let Some(bare) = bare { - symbols.add_bare_import(bare); + if let FrozenUnit::Import(path, _, span) = unit { + let _ = symbols.insert(path.as_str(), SymbolType::Import, Some(*span)); + if let Some(import) = Import::of(unit) { + symbols.add_import(import); } continue; } @@ -2484,11 +2481,11 @@ fn validate_type( // and `field: SomeValidator` all type-checked, and adding errors // to the table would have quietly extended that to them. // - // A name the bare-import set brought into scope is opaque - the + // A name a `use` brought into scope is opaque - the // schema it came from is not necessarily parsed here - so it is // accepted without a kind. let found = symbols.get(name); - let imported_bare = symbols.is_imported_bare(name); + let imported = symbols.is_imported(name); if let Some(kind) = found.filter(|k| !k.usable_as_type()) { errors.push( @@ -2499,7 +2496,7 @@ fn validate_type( .with_context(context.to_string()) .with_span(span), ); - } else if found.is_none() && !imported_bare { + } else if found.is_none() && !imported { // A text-type hint says exactly what to write; a "did you // mean" alongside it would only contradict it (`str` is one // edit from `s8`). diff --git a/core/tests/schema/ir/type_resolution.rs b/core/tests/schema/ir/type_resolution.rs index ee94d26..ad8c1b5 100644 --- a/core/tests/schema/ir/type_resolution.rs +++ b/core/tests/schema/ir/type_resolution.rs @@ -40,14 +40,21 @@ fn shape(context: &ProjectContext, protocol: &str) -> String { .clone() } -/// What `User` takes under `packed@1`, written in the schema `namespace`. -fn size_of(context: &ProjectContext, namespace: &str, reference: &str) -> Option { +/// What `reference` takes under `packed@1`, written in the schema `namespace`; +/// `packages` are the names of the dependencies the context has. +fn size_of( + context: &ProjectContext, + namespace: &str, + reference: &str, + packages: &[&str], +) -> Option { let frozen: Vec> = context .schema_contexts .iter() .filter_map(|s| s.borrow().frozen_schema.borrow().clone()) .collect(); - let lookup = FrozenLookup::new(frozen.iter().map(Vec::as_slice)); + let lookup = + FrozenLookup::with_packages(frozen.iter().map(Vec::as_slice), packages.iter().copied()); let scope = lookup.scope_of(namespace).expect("the schema"); size::range( &TypeRef::Named(reference.to_string()), @@ -62,7 +69,10 @@ fn size_of(context: &ProjectContext, namespace: &str, reference: &str) -> Option fn declared_here(user: &str) -> (String, Option) { let source = format!("{user}\n{}", users("", "User")); let context = compile(&[(&["app"], source)]); - (shape(&context, "Users"), size_of(&context, "app", "User")) + ( + shape(&context, "Users"), + size_of(&context, "app", "User", &[]), + ) } /// Schemas that hold the same name under namespaces that resemble each other, @@ -121,7 +131,7 @@ fn a_name_resolves_to_the_declaration_the_schema_imports() { if shape != expected.0 { wrong.push(format!("compat: {label}, imports {path}, {form}")); } - if size_of(&context, "app", &reference) != expected.1 { + if size_of(&context, "app", &reference, &[]) != expected.1 { wrong.push(format!("sizer: {label}, imports {path}, {form}")); } } @@ -182,6 +192,15 @@ fn dependency_shape(field: &str, decoy: bool) -> String { shape(&context, "Q") } +/// What `B` takes where `p1` imports it, as the sizer reads it. +#[cfg(feature = "deps")] +fn dependency_size(field: &str, decoy: bool) -> Option { + let tmp = consumer(field, decoy); + let context = + comline_core::package::build::compile_package(&tmp.path().join("p1")).expect("p1 builds"); + size_of(&context, "s0", "B", &["p0", "std"]) +} + #[cfg(feature = "deps")] #[test] fn a_dependencys_bare_name_is_not_the_consumers_declaration() { @@ -196,3 +215,13 @@ fn a_dependencys_qualified_name_is_not_the_consumers_declaration() { dependency_shape("s0::A", false) ); } + +#[cfg(feature = "deps")] +#[test] +fn the_sizer_reads_a_dependencys_names_in_its_package() { + for field in ["A", "s0::A"] { + let alone = dependency_size(field, false); + assert!(alone.is_some(), "`{field}` is sized"); + assert_eq!(dependency_size(field, true), alone, "`{field}`"); + } +}