diff --git a/core/Cargo.toml b/core/Cargo.toml index 83c27e2..18e30ba 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -27,7 +27,7 @@ comline-ir-formats = { path = "formats" } # The specification's catalog of shared names: guarantees, facts and their # values, the official encodings' aliases and frozen spellings. no_std and # dependency-free, so the IR-only build has it too. -comline-catalog = { git = "https://github.com/ComlineProject/specification", rev = "6d39fc363ab3dce9d1c4876b0d414edb375e25e9" } +comline-catalog = { git = "https://github.com/ComlineProject/specification", rev = "16dfe40d835b85b45de07131181339fe3e9f0ccb" } # General eyre = "0.6.8" diff --git a/core/examples/imports/src/hole.ids b/core/examples/imports/src/hole.ids index eea70c1..f8cfaa1 100644 --- a/core/examples/imports/src/hole.ids +++ b/core/examples/imports/src/hole.ids @@ -1,10 +1,10 @@ // Imports whole schemas by namespace -use ant -use food +use package::ant +use package::food // Or imports exactly the components you want: structures, enums, errors -use ant::{Ant, Mood, CantCarryError} -use food::{Food, State, NotEdibleError} +use package::ant::{Ant, Mood, CantCarryError} +use package::food::{Food, State, NotEdibleError} protocol Gathering { function gather(ant::Ant) -> food::Food; diff --git a/core/oracle/src/model.rs b/core/oracle/src/model.rs index c0fe4e0..9bc3ac6 100644 --- a/core/oracle/src/model.rs +++ b/core/oracle/src/model.rs @@ -2,7 +2,7 @@ //! //! Packages are `p0`, `p1`, ... and each depends only on lower ones, so the //! graph is a DAG by construction. A package's schemas are `src/s0.ids`, -//! `src/s1.ids`, ...; every package has an `s0`, and a `use s0::T` names the +//! `src/s1.ids`, ...; every package has an `s0`, and a `use package::s0::T` names the //! schema of the package it is written in; a schema holds structs and //! protocols. Type and protocol names are unique across the whole graph //! (`P1S0T2`, `P1S0Q0`): an acceptance names a protocol and nothing else. @@ -291,7 +291,7 @@ impl Model { for ty in tys { if let Ty::Struct { of, .. } = ty { if (of.pkg, of.schema) != (pkg, schema) { - let path = if of.pkg == pkg { format!("s{}", of.schema) } else { format!("p{}::s{}", of.pkg, of.schema) }; + let path = if of.pkg == pkg { format!("package::s{}", of.schema) } else { format!("p{}::s{}", of.pkg, of.schema) }; uses.insert(format!("use {path}::{}\n", self.type_name(*of))); } } diff --git a/core/src/doc.rs b/core/src/doc.rs index c95c3e5..2a4401b 100644 --- a/core/src/doc.rs +++ b/core/src/doc.rs @@ -1346,8 +1346,8 @@ mod tests { type CustomerId = u64\n"; const API: &str = "//! The shop's API.\n\n\ - use types::Customer\n\ - use types::Payment\n\n\ + use package::types::Customer\n\ + use package::types::Payment\n\n\ /// Not in stock.\nerror OutOfStock {\n message = f\"{item} is out of stock\"\n item: string\n}\n\n\ /// One order.\nstruct Order {\n customer: Customer\n payments: Payment[]\n lines: union(u32 string)\n}\n\n\ /// Orders things.\nprotocol Orders {\n \ diff --git a/core/src/schema/ir/compiler/import_resolver.rs b/core/src/schema/ir/compiler/import_resolver.rs index 9e558ef..22e4ed4 100644 --- a/core/src/schema/ir/compiler/import_resolver.rs +++ b/core/src/schema/ir/compiler/import_resolver.rs @@ -13,7 +13,7 @@ use crate::schema::idl::constants::SCHEMA_EXTENSION; use crate::schema::idl::grammar::{Declaration, UsePath, RelativePrefix}; use crate::schema::ir::context::SchemaContext; use crate::utils::text::closest; -use crate::diagnostics::{Diagnostic, DiagnosticCode}; +use crate::diagnostics::{Diagnostic, DiagnosticCode, Fix}; use crate::schema::ir::frozen::types::{Literal, TypeRef}; use crate::schema::ir::validation::constants::{fits, literal_value, primitive_bound, ResolvedConstant}; use crate::schema::ir::frozen::unit::FrozenUnit; @@ -23,6 +23,13 @@ use crate::schema::ir::validation::data::{carries_data, Answers}; const UNRESOLVED: DiagnosticCode = DiagnosticCode::error("import::unresolved"); +/// A `use` path that reaches one of the package's own schemas without +/// `package::`, `parent::` or `self::`. An absolute first segment means `std` +/// or a dependency, so the path is an error where it reaches one. The code is +/// in the specification's compiler cases (`imports`). +pub const BARE_OWN_SCHEMA_PATH: DiagnosticCode = + DiagnosticCode::error("import::bare-own-schema-path"); + /// Resolved import information #[derive(Debug, Clone)] pub struct ResolvedImport { @@ -188,7 +195,11 @@ impl ImportResolver { Some(self.resolve_relative_prefix(&prefix, &parts[1..], current_namespace)) } - /// Resolve absolute path (e.g., std::http::Request or mypackage::types::User) + /// Resolve absolute path (e.g., `std::http::Request` or a dependency's + /// `shared::models::Thing`). A package's own schemas are reached only + /// through `package::`, `parent::` or `self::`: what falls through to + /// "the same package" below is the resolver's reading of a bare path, and + /// [`check_imports`] refuses it where it reaches one of them. fn resolve_absolute( &self, path: String, @@ -223,9 +234,13 @@ impl ImportResolver { }); } - // It's from the same package - already loaded as a SchemaContext in the - // ProjectContext being compiled, so no schema_path is needed to load it - // from disk; see `resolve_use_to_schema` below for how it's located. + // Neither `std` nor a dependency: a bare path, which names a schema of + // the same package when there is one (already loaded as a SchemaContext + // in the ProjectContext being compiled, so no schema_path is needed to + // load it from disk; see `resolve_use_to_schema` below for how it's + // located). `check_imports` makes that an error + // (`import::bare-own-schema-path`): own schemas are reached through + // `package::`, `parent::` or `self::`. Ok(ResolvedImport { absolute_namespace: parts, schema_path: None, @@ -877,6 +892,83 @@ pub(crate) fn importable_names(schema_context: &SchemaContext) -> Vec { names } +/// The first segment of a `use` path as written, when it is an absolute one: +/// `None` for a path that starts with `self`, `parent` or `package`, which is +/// relative. A bare path's first segment is `std`, a dependency's name, or +/// (the error [`BARE_OWN_SCHEMA_PATH`]) one of the package's own schemas. +pub fn absolute_first_segment(use_path: &UsePath) -> Option<&str> { + let path = match use_path { + UsePath::Absolute(scoped) => scoped.as_str(), + UsePath::Glob(glob) => glob.path.as_str(), + UsePath::Multi(multi) => multi.path.as_str(), + UsePath::Relative(_) | UsePath::RelativeGlob(_) | UsePath::RelativeMulti(_) => return None, + }; + let first = path.split("::").next().unwrap_or(path); + (!matches!(first, "self" | "parent" | "package")).then_some(first) +} + +/// A `use` path as it is written, without its alias. +fn written_path(use_path: &UsePath) -> String { + match use_path { + UsePath::Absolute(scoped) => scoped.to_string(), + UsePath::Relative(rel) => format!("{}::{}", rel.prefix.keyword(), rel.path.as_str()), + UsePath::Glob(glob) => format!("{}::*", glob.path.as_str()), + UsePath::Multi(multi) => { + let items: Vec<&str> = multi.items.iter().map(|item| item.text.as_str()).collect(); + format!("{}::{{{}}}", multi.path.as_str(), items.join(", ")) + } + UsePath::RelativeGlob(glob) => format!("{}::*", glob.prefix.keyword()), + UsePath::RelativeMulti(multi) => { + let items: Vec<&str> = multi.items.iter().map(|item| item.text.as_str()).collect(); + format!("{}::{{{}}}", multi.prefix.keyword(), items.join(", ")) + } + } +} + +/// The error for a bare path to one of the package's own schemas, with the +/// fix that writes `package::` in front of it. `span` is the `use` +/// declaration's, and `source` the schema's text: without it the error stands +/// without a fix. The fix is the whole of the rewrite: what the import brings +/// in is the schema's namespace, whichever path reached it, so a reference +/// written `models::Blob` after `use models` stays right after +/// `use package::models`. +fn bare_own_schema_path( + use_path: &UsePath, + first: &str, + span: (usize, usize), + source: Option<&str>, +) -> Diagnostic { + let written = written_path(use_path); + let write = format!("package::{written}"); + let diagnostic = Diagnostic::new( + BARE_OWN_SCHEMA_PATH, + format!("'{written}' is one of this package's own schemas, reached by a bare path"), + ) + .with_context(format!( + "an absolute path starts with `std` or a dependency's name; '{first}' is neither" + )) + .with_span(span) + .with_help(format!( + "write `{write}`: a package's own schemas are reached as `package::...`, `parent::...` or `self::...`" + )); + match own_path_fix(source, span, first, &write) { + Some(fix) => diagnostic.with_fix(fix), + None => diagnostic, + } +} + +/// Inserting `package::` where the `use` declaration at `span` names `first`, +/// the first segment of its path; `None` when `source` isn't there, or the +/// declaration isn't `use`, whitespace, and then `first`. +fn own_path_fix(source: Option<&str>, span: (usize, usize), first: &str, write: &str) -> Option { + let source = source?; + let after_use = source.get(span.0..span.1)?.strip_prefix("use")?; + let at = span.1 - after_use.trim_start().len(); + source.get(at..)?.starts_with(first).then(|| { + Fix::replace(format!("Write `{write}`"), (at, at), "package::") + }) +} + /// Every `use` in `declarations` that doesn't resolve, as validation /// errors: a path no schema of the project matches (its own, or a /// dependency's, merged under the dependency's name), or a named item the @@ -884,6 +976,11 @@ pub(crate) fn importable_names(schema_context: &SchemaContext) -> Vec { /// before a schema is compiled, so `comline build` / `check` reject the import /// instead of trusting it. /// +/// A path that starts with neither `self`, `parent`, `package`, `std` nor a +/// dependency's name, and reaches one of the package's own schemas, is the +/// error [`BARE_OWN_SCHEMA_PATH`], with a fix that writes `package::` in front +/// of it. A path that reaches nothing is `import::unresolved`, as it was. +/// /// A `std::` path is checked like any other: the build merges the std /// schemas a package imports (see `package::stdlib`), and one that no std /// schema matches gets a suggestion from std's namespaces. @@ -928,6 +1025,15 @@ pub fn check_imports( let resolver = ImportResolver::new(vec![], Default::default(), None); let mut errors = Vec::new(); + // The schema's text, for the fix a bare path to an own schema carries. + let source: Option = project_context + .schema_contexts + .iter() + .find(|schema| schema.borrow().namespace == current_namespace) + .and_then(|schema| { + schema.borrow().codemap.files().first().map(|file| file.contents().to_string()) + }); + for decl in declarations { let Declaration::Use(use_stmt) = &decl.value else { continue; @@ -974,6 +1080,16 @@ pub fn check_imports( continue; }; + // A path that starts with neither `std` nor a dependency's name, and + // reaches one of the package's own schemas: those are only reached + // through `package::`, `parent::` or `self::`. + if let Some(written) = absolute_first_segment(&use_stmt.path) { + if written != stdlib::NAMESPACE && !dependencies.contains(written) { + errors.push(bare_own_schema_path(&use_stmt.path, written, decl.span, source.as_deref())); + continue; + } + } + let schema = schema.borrow(); let named: Vec = if target.resolved.symbols == ["*"] { vec![] diff --git a/core/tests/cas/dependency_vendoring_tests.rs b/core/tests/cas/dependency_vendoring_tests.rs index 0d5da2f..5534ac0 100644 --- a/core/tests/cas/dependency_vendoring_tests.rs +++ b/core/tests/cas/dependency_vendoring_tests.rs @@ -176,7 +176,7 @@ fn an_unchanged_dependency_contributes_no_bump() { fn a_dependency_whose_schemas_import_each_other_still_builds() { let workspace = TempDir::new().unwrap(); let dependency = workspace.path().join("shared-types"); - write_dependency(&dependency, "use common::Base\n\nstruct Thing {\n base: Base\n}\n"); + write_dependency(&dependency, "use package::common::Base\n\nstruct Thing {\n base: Base\n}\n"); fs::write(dependency.join("src/common.ids"), "struct Base {\n id: u64\n}\n").unwrap(); let consumer_dir = workspace.path().join("consumer"); write_consumer(&consumer_dir, CONSUMER_SCHEMA); @@ -233,7 +233,7 @@ fn a_dependency_is_taken_as_its_own_compile_froze_it() { write_dependency(&dependency, "struct Thing {\n id: u64\n}\n"); fs::write( dependency.join("src/api.ids"), - "use models::Thing\n\nprotocol Things {\n function get(id: u64) -> Thing;\n}\n", + "use package::models::Thing\n\nprotocol Things {\n function get(id: u64) -> Thing;\n}\n", ) .unwrap(); let consumer = workspace.path().join("consumer"); diff --git a/core/tests/package/from_sources.rs b/core/tests/package/from_sources.rs index 25f5a23..a54a105 100644 --- a/core/tests/package/from_sources.rs +++ b/core/tests/package/from_sources.rs @@ -55,9 +55,9 @@ fn cross_schema_use_resolves_across_the_added_schemas() { // The bare name (`use ns::Name` -> `Name`), the qualified form (`ns::Name`), // and an alias (`use ns::Name as X` -> `X`) all resolve. let cases = [ - ("use types::User", "User"), - ("use types::User", "types::User"), - ("use types::User as Account", "Account"), + ("use package::types::User", "User"), + ("use package::types::User", "types::User"), + ("use package::types::User as Account", "Account"), ]; for (import, reference) in cases { let ctx = PackageSources::new() @@ -524,7 +524,7 @@ fn an_imported_validator_without_a_default_needs_its_argument() { let build = |call: &str| { PackageSources::new() .schema(["rules"], rules) - .schema(["api"], &format!("use rules::AtLeast\n\nstruct S {{\n @validators = [{call}]\n n: u32\n}}\n")) + .schema(["api"], &format!("use package::rules::AtLeast\n\nstruct S {{\n @validators = [{call}]\n n: u32\n}}\n")) .compile() .err() .map(|e| e.to_string()) @@ -550,7 +550,7 @@ fn an_imported_alias_resolves_before_matching() { .schema(["types"], types) .schema( ["api"], - "use types::{AccountName, NonEmpty}\n\nstruct S {\n @validators = [NonEmpty()]\n name: AccountName\n}\n", + "use package::types::{AccountName, NonEmpty}\n\nstruct S {\n @validators = [NonEmpty()]\n name: AccountName\n}\n", ) .compile() .expect("an alias of string matches `for string`"); @@ -559,7 +559,7 @@ fn an_imported_alias_resolves_before_matching() { .schema(["types"], types) .schema( ["api"], - "use types::{Count, NonEmpty}\n\nstruct S {\n @validators = [NonEmpty()]\n n: Count\n}\n", + "use package::types::{Count, NonEmpty}\n\nstruct S {\n @validators = [NonEmpty()]\n n: Count\n}\n", ) .compile() .err() diff --git a/core/tests/package/unverified.rs b/core/tests/package/unverified.rs index 8b6b4f1..d369bb7 100644 --- a/core/tests/package/unverified.rs +++ b/core/tests/package/unverified.rs @@ -373,7 +373,7 @@ const MARKED_ORDERS: &str = fn an_unbounded_type_from_another_schema_is_unbounded_where_used() { let err = compile_with( &[("models", MODELS)], - &format!("use models::Blob\n\n{ORDERS}"), + &format!("use package::models::Blob\n\n{ORDERS}"), ) .unwrap_err() .to_string(); @@ -387,7 +387,7 @@ fn an_unbounded_type_from_another_schema_is_unbounded_where_used() { fn a_marked_protocol_using_it_gives_up_bounded_size() { let units = compile_with( &[("models", MODELS)], - &format!("use models::Blob\n\n{MARKED_ORDERS}"), + &format!("use package::models::Blob\n\n{MARKED_ORDERS}"), ) .unwrap(); assert_eq!( @@ -401,7 +401,7 @@ fn a_type_bounded_where_declared_is_bounded_where_used() { let models = MODELS.replace("data: string", "data: char[max 64]"); let units = compile_with( &[("models", &models)], - &format!("use models::Blob\n\n{ORDERS}"), + &format!("use package::models::Blob\n\n{ORDERS}"), ) .unwrap(); assert!(unverified::frozen_set(protocol_params(&units, "Orders")).is_empty()); @@ -409,7 +409,7 @@ fn a_type_bounded_where_declared_is_bounded_where_used() { #[test] fn it_stays_unbounded_through_a_rename_a_struct_and_another_schema() { - let renamed = "use models::Blob as Payload\n\n\ + let renamed = "use package::models::Blob as Payload\n\n\ protocol Orders {\n function send(b: Payload);\n}\n"; let err = compile_with(&[("models", MODELS)], renamed) .unwrap_err() @@ -421,7 +421,7 @@ fn it_stays_unbounded_through_a_rename_a_struct_and_another_schema() { // `Order` holds a `Blob`; no protocol here reaches it, so another // schema could use it, and it has to be bounded. - let held = "use models::Blob\n\nstruct Order {\n blob: Blob\n}\n"; + let held = "use package::models::Blob\n\nstruct Order {\n blob: Blob\n}\n"; let err = compile_with(&[("models", MODELS)], held) .unwrap_err() .to_string(); @@ -432,9 +432,9 @@ fn it_stays_unbounded_through_a_rename_a_struct_and_another_schema() { // `wrap` holds it in `Wrapper`, reached only by its own marked // protocol; `api` uses `Wrapper`. - let wrap = "use models::Blob\n\nstruct Wrapper {\n blob: Blob\n}\n\n\ + let wrap = "use package::models::Blob\n\nstruct Wrapper {\n blob: Blob\n}\n\n\ @unverified = \"wraps uploads\"\nprotocol Wrap {\n function wrap(w: Wrapper);\n}\n"; - let api = "use wrap::Wrapper\n\nprotocol Orders {\n function send(w: Wrapper);\n}\n"; + let api = "use package::wrap::Wrapper\n\nprotocol Orders {\n function send(w: Wrapper);\n}\n"; let err = compile_with(&[("wrap", wrap), ("models", MODELS)], api) .unwrap_err() .to_string(); diff --git a/core/tests/package/unverified_inner.rs b/core/tests/package/unverified_inner.rs index a389acf..d022dc2 100644 --- a/core/tests/package/unverified_inner.rs +++ b/core/tests/package/unverified_inner.rs @@ -203,7 +203,7 @@ fn a_struct_marker_is_a_type_marker_too() { fn a_type_in_another_schema_of_the_package_is_covered_by_its_marker() { let models = format!("@unverified(bounded_size = \"{WHY}\")\nstruct Blob {{\n data: string\n}}\n"); - let api = "use models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n"; + let api = "use package::models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n"; let ctx = PackageSources::new() .config(&config(ALLOW)) .schema(["api"], api) diff --git a/core/tests/package/unverified_oracle.rs b/core/tests/package/unverified_oracle.rs index 00ed949..a999bbf 100644 --- a/core/tests/package/unverified_oracle.rs +++ b/core/tests/package/unverified_oracle.rs @@ -183,7 +183,7 @@ fn an_upstream_field_made_unbounded_is_not_accepted_by_a_consumer_that_accepted_ expect(&m, false); } -/// Regression test for core#245: a dependency's `use s0::A`, naming a +/// Regression test for core#245: a dependency's `use package::s0::A`, naming a /// sibling schema of its own package, resolves within that package, not /// against the consumer's schema of the same name. Two packages that each have /// an `s0` and import across them used to be reported as an import cycle @@ -194,7 +194,7 @@ fn a_dependencys_use_of_its_sibling_schema_is_not_the_consumers() { let files = [ ("p0/config.idp", "congregation p0\nspecification_version = 1\n"), ("p0/src/s0.ids", "struct A {\n f0: char[max 64]\n}\n"), - ("p0/src/s1.ids", "use s0::A\n\nstruct B {\n f0: A\n}\n"), + ("p0/src/s1.ids", "use package::s0::A\n\nstruct B {\n f0: A\n}\n"), ( "p1/config.idp", "congregation p1\nspecification_version = 1\ndependencies = {\n p0 = {\n path = \"../p0\"\n }\n}\n", diff --git a/core/tests/package/unverified_strict.rs b/core/tests/package/unverified_strict.rs index fb69736..b3f4de9 100644 --- a/core/tests/package/unverified_strict.rs +++ b/core/tests/package/unverified_strict.rs @@ -340,7 +340,7 @@ fn strict_a_marker_on_a_type_used_twice_is_innermost() { #[test] fn strict_a_type_in_another_schema_of_the_package_is_judged_where_it_is_declared() { let models = format!("@unverified(bounded_size = \"{WHY}\")\ntype Blob = string\n"); - let api = "use models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n"; + let api = "use package::models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n"; PackageSources::new() .config(config(STRICT)) .schema(["api"], api) diff --git a/core/tests/schema/ir/alias_validators.rs b/core/tests/schema/ir/alias_validators.rs index 55cbb28..5d32504 100644 --- a/core/tests/schema/ir/alias_validators.rs +++ b/core/tests/schema/ir/alias_validators.rs @@ -156,7 +156,7 @@ impl ImportLookup for StandIn { #[test] fn a_stand_in_import_is_not_judged_against_a_validator() { - let code = "use rules::Name\n\nvalidator Positive for u32 {\n validate {\n \ + let code = "use package::rules::Name\n\nvalidator Positive for u32 {\n validate {\n \ assert(value > 0, f\"{value.name}\")\n }\n}\n\ struct S {\n @validators = [Positive()]\n n: Name\n}\n"; assert_eq!(messages_with(code, &StandIn), Vec::::new()); diff --git a/core/tests/schema/ir/dataless_arrays.rs b/core/tests/schema/ir/dataless_arrays.rs index cc4ca40..8615e34 100644 --- a/core/tests/schema/ir/dataless_arrays.rs +++ b/core/tests/schema/ir/dataless_arrays.rs @@ -154,8 +154,8 @@ fn project() -> ProjectContext { fn an_imported_data_less_type_is_refused_through_aliases_and_schemas() { let mut project = project(); add_schema(&mut project, &["empty"], "struct Empty {}\n"); - add_schema(&mut project, &["types"], "use empty::Empty\n\ntype Nothing = Empty\n"); - add_schema(&mut project, &["api"], "use types::Nothing\n\nstruct Batch {\n items: Nothing[]\n}\n"); + add_schema(&mut project, &["types"], "use package::empty::Empty\n\ntype Nothing = Empty\n"); + add_schema(&mut project, &["api"], "use package::types::Nothing\n\nstruct Batch {\n items: Nothing[]\n}\n"); let error = interpret_context(&project).expect_err("the array is refused").to_string(); assert!(error.contains("an array of `Nothing` carries only its length"), "{error}"); @@ -168,7 +168,7 @@ fn an_imported_type_with_data_is_not_refused() { add_schema( &mut project, &["api"], - "use types::Node\nuse types::Flag\n\nstruct Batch {\n nodes: Node[max 8]\n flags: Flag[max 8]\n}\n", + "use package::types::Node\nuse package::types::Flag\n\nstruct Batch {\n nodes: Node[max 8]\n flags: Flag[max 8]\n}\n", ); interpret_context(&project).expect("arrays of types that carry data build"); diff --git a/core/tests/schema/ir/imports.rs b/core/tests/schema/ir/imports.rs index ff98650..5b5295c 100644 --- a/core/tests/schema/ir/imports.rs +++ b/core/tests/schema/ir/imports.rs @@ -51,7 +51,7 @@ fn test_whole_schema_use_resolves_across_files() { add_schema( &mut project, &["api"], - "use types\n\nstruct Response {\n id: u64\n}\n", + "use package::types\n\nstruct Response {\n id: u64\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -73,7 +73,7 @@ fn test_symbol_use_resolves_to_declaring_schema() { add_schema( &mut project, &["api"], - "use types::User\n\nstruct Response {\n id: u64\n}\n", + "use package::types::User\n\nstruct Response {\n id: u64\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -99,7 +99,7 @@ fn test_multi_item_use_resolves_each_symbol() { add_schema( &mut project, &["api"], - "use types::{User, Post}\n\nstruct Response {\n id: u64\n}\n", + "use package::types::{User, Post}\n\nstruct Response {\n id: u64\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -124,7 +124,7 @@ fn test_glob_use_resolves_namespace() { add_schema( &mut project, &["api"], - "use types::*\n\nstruct Response {\n id: u64\n}\n", + "use package::types::*\n\nstruct Response {\n id: u64\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -142,8 +142,8 @@ fn test_glob_use_resolves_namespace() { #[test] fn test_import_cycle_between_schemas_is_rejected() { let mut project = build_project(); - add_schema(&mut project, &["a"], "use b\n\nstruct A {\n id: u64\n}\n"); - add_schema(&mut project, &["b"], "use a\n\nstruct B {\n id: u64\n}\n"); + add_schema(&mut project, &["a"], "use package::b\n\nstruct A {\n id: u64\n}\n"); + add_schema(&mut project, &["b"], "use package::a\n\nstruct B {\n id: u64\n}\n"); let result = interpret_context(&project); assert!(result.is_err(), "Expected a cycle error, got {:?}", result); @@ -172,7 +172,7 @@ fn test_same_package_symbol_not_found_is_rejected() { add_schema( &mut project, &["api"], - "use types::Missing\n\nstruct Response {\n id: u64\n}\n", + "use package::types::Missing\n\nstruct Response {\n id: u64\n}\n", ); // Builds used to trust this; an import of something that doesn't exist @@ -197,7 +197,7 @@ fn test_unknown_namespace_is_rejected_with_a_suggestion() { fn test_multi_item_use_rejects_only_the_undeclared_item() { let mut project = build_project(); add_schema(&mut project, &["types"], "struct User {\n id: u64\n}\n"); - add_schema(&mut project, &["api"], "use types::{User, Nope}\n\nstruct R {\n u: User\n}\n"); + add_schema(&mut project, &["api"], "use package::types::{User, Nope}\n\nstruct R {\n u: User\n}\n"); let error = interpret_context(&project).expect_err("an unresolved import should fail").to_string(); assert!(error.contains("doesn't declare 'Nope'"), "{error}"); @@ -307,7 +307,7 @@ fn test_bare_prefix_glob_of_a_missing_schema_is_rejected() { fn test_importing_an_error_resolves() { let mut project = build_project(); add_schema(&mut project, &["errs"], "error Gone {\n message = \"gone\"\n}\n"); - add_schema(&mut project, &["api"], "use errs::Gone\n\nstruct R {\n id: u64\n}\n"); + add_schema(&mut project, &["api"], "use package::errs::Gone\n\nstruct R {\n id: u64\n}\n"); interpret_context(&project).expect("an error is importable"); } @@ -356,11 +356,11 @@ fn test_a_typo_in_a_dependency_name_is_rejected_with_a_suggestion() { #[test] fn test_a_dependency_schemas_own_imports_are_not_checked_in_the_consumer() { - // Merged under its name, a dependency's own `use foo::X` (relative to - // the dependency) doesn't line up with the consumer's namespaces; it was + // Merged under its name, a dependency's own `use package::foo::X` (relative + // to the dependency) doesn't line up with the consumer's namespaces; it was // checked when the dependency was compiled on its own. let mut project = build_project_with_dependency(); - add_schema(&mut project, &["shared", "bar"], "use foo::X\n\nstruct B {\n x: X\n}\n"); + add_schema(&mut project, &["shared", "bar"], "use package::foo::X\n\nstruct B {\n x: X\n}\n"); interpret_context(&project).expect("a dependency's own imports pass"); } @@ -372,7 +372,7 @@ fn test_whole_namespace_import_allows_qualified_field_reference() { add_schema( &mut project, &["api"], - "use types\n\nstruct Response {\n user: types::User\n}\n", + "use package::types\n\nstruct Response {\n user: types::User\n}\n", ); interpret_context(&project).expect( @@ -388,7 +388,7 @@ fn test_glob_import_allows_qualified_field_reference() { add_schema( &mut project, &["api"], - "use types::*\n\nstruct Response {\n user: types::User\n}\n", + "use package::types::*\n\nstruct Response {\n user: types::User\n}\n", ); interpret_context(&project).expect( @@ -440,7 +440,7 @@ fn test_use_binds_the_bare_name() { &mut project, &["api"], // bare `User`, not `types::User` - "use types::User\n\nstruct Session {\n user: User\n}\n", + "use package::types::User\n\nstruct Session {\n user: User\n}\n", ); interpret_context(&project).expect("a bare reference after `use` should resolve"); @@ -453,7 +453,7 @@ fn test_use_as_binds_the_alias() { add_schema( &mut project, &["api"], - "use types::User as Account\n\nstruct Session {\n who: Account\n}\n", + "use package::types::User as Account\n\nstruct Session {\n who: Account\n}\n", ); interpret_context(&project).expect("the alias should resolve as a type"); @@ -478,7 +478,7 @@ fn test_use_as_does_not_bind_the_original_bare_name() { &mut project, &["api"], // aliased, so bare `User` is NOT in scope — only `Account` (or `types::User`) - "use types::User as Account\n\nstruct Session {\n who: User\n}\n", + "use package::types::User as Account\n\nstruct Session {\n who: User\n}\n", ); assert!( @@ -527,7 +527,7 @@ fn test_thrown_foreign_error_gets_a_reexport_slot() { add_schema( &mut project, &["api"], - "use errs::NotFound\n\nprotocol Store {\n function get(u64) -> char[max 64] ! NotFound;\n}\n", + "use package::errs::NotFound\n\nprotocol Store {\n function get(u64) -> char[max 64] ! NotFound;\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -567,7 +567,7 @@ fn test_local_errors_keep_low_ordinals_foreign_reexports_come_after() { add_schema( &mut project, &["api"], - "use errs::Denied\n\n\ + "use package::errs::Denied\n\n\ error Local {\n message = \"local\"\n}\n\ protocol Store {\n function act(u64) -> char[max 64] ! Denied;\n}\n", ); @@ -615,7 +615,7 @@ fn test_cross_file_type_alias_keeps_its_name_via_single_symbol_use() { add_schema( &mut project, &["api"], - "use types::UserId\n\nstruct Response {\n id: UserId\n}\n", + "use package::types::UserId\n\nstruct Response {\n id: UserId\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -641,7 +641,7 @@ fn test_cross_file_type_alias_keeps_its_name_via_whole_namespace_use() { &["api"], // bare `id: UserId`, not `id: types::UserId` - exercises the // `declared_symbol_names` whole-namespace expansion path. - "use types\n\nstruct Response {\n id: UserId\n}\n", + "use package::types\n\nstruct Response {\n id: UserId\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -659,11 +659,11 @@ fn test_cross_file_alias_chain_through_two_files_keeps_the_written_name() { let mut project = build_project(); add_schema(&mut project, &["a"], "type Id = u64\n"); - add_schema(&mut project, &["b"], "use a::Id\n\ntype ForeignId = Id\n"); + add_schema(&mut project, &["b"], "use package::a::Id\n\ntype ForeignId = Id\n"); add_schema( &mut project, &["c"], - "use b::ForeignId\n\nstruct Response {\n id: ForeignId\n}\n", + "use package::b::ForeignId\n\nstruct Response {\n id: ForeignId\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -689,7 +689,7 @@ fn test_cross_file_alias_target_that_is_a_real_struct_is_not_inlined() { &["api"], // `User` is a real struct, not a `type` alias - must be left as a // by-name reference, not inlined into its field list. - "use types::User\n\nstruct Response {\n id: User\n}\n", + "use package::types::User\n\nstruct Response {\n id: User\n}\n", ); interpret_context(&project).expect("compilation should succeed"); @@ -741,19 +741,19 @@ fn an_imported_constant_is_checked_where_it_is_named() { add_schema( &mut project, &["api"], - "use limits::MAX\nuse limits::SMALL\n\nconst OWN: u32 = MAX\n\nstruct R {\n a: u32 = MAX\n b: u8 = SMALL\n}\n", + "use package::limits::MAX\nuse package::limits::SMALL\n\nconst OWN: u32 = MAX\n\nstruct R {\n a: u32 = MAX\n b: u8 = SMALL\n}\n", ); interpret_context(&project).expect("values that fit should build"); let mut project = build_project(); add_schema(&mut project, &["limits"], "const BASE: u32 = 500\nconst MAX: u32 = BASE\n"); - add_schema(&mut project, &["api"], "use limits::MAX\n\nstruct R {\n a: u8 = MAX\n}\n"); + add_schema(&mut project, &["api"], "use package::limits::MAX\n\nstruct R {\n a: u8 = MAX\n}\n"); let error = interpret_context(&project).expect_err("500 doesn't fit a u8").to_string(); assert!(error.contains("`MAX` is 500, which does not fit in `u8`"), "{error}"); let mut project = build_project(); add_schema(&mut project, &["limits"], "const MAX: u32 = 500\n"); - add_schema(&mut project, &["api"], "use limits\n\nconst OWN: u8 = limits::MAX\n"); + add_schema(&mut project, &["api"], "use package::limits\n\nconst OWN: u8 = limits::MAX\n"); let error = interpret_context(&project).expect_err("500 doesn't fit a u8").to_string(); assert!(error.contains("`limits::MAX` is 500, which does not fit in `u8`"), "{error}"); } @@ -763,7 +763,7 @@ fn an_imported_constant_is_checked_where_it_is_named() { fn an_imported_constant_sizes_an_array() { let mut project = build_project(); add_schema(&mut project, &["limits"], "const NAME: u32 = 24\n"); - add_schema(&mut project, &["api"], "use limits::NAME\n\nstruct R {\n name: char[max NAME]\n}\n"); + add_schema(&mut project, &["api"], "use package::limits::NAME\n\nstruct R {\n name: char[max NAME]\n}\n"); interpret_context(&project).expect("an imported size should build"); let frozen = frozen_units_for(&project, "api"); let ty = frozen.iter().find_map(|u| match u { @@ -777,8 +777,141 @@ fn an_imported_constant_sizes_an_array() { let mut project = build_project(); add_schema(&mut project, &["limits"], "const NAME: s32 = 24\n"); - add_schema(&mut project, &["api"], "use limits::NAME\n\nstruct R {\n name: char[max NAME]\n}\n"); + add_schema(&mut project, &["api"], "use package::limits::NAME\n\nstruct R {\n name: char[max NAME]\n}\n"); let error = interpret_context(&project).expect_err("a signed size is refused").to_string(); assert!(error.contains("the maximum `NAME` isn't an unsigned integer constant"), "{error}"); } + +// ---- Own schemas are reached only through `package::`, `parent::` or `self::`. + +fn bare_path_errors(project: &ProjectContext, namespace: &[&str]) -> Vec { + use comline_core::schema::ir::compiler::import_resolver::check_imports; + + let namespace: Vec = namespace.iter().map(|s| s.to_string()).collect(); + let schema = project + .schema_contexts + .iter() + .find(|schema| schema.borrow().namespace == namespace) + .expect("schema should exist"); + let declarations = schema.borrow().declarations.clone(); + check_imports(&declarations, &namespace, project).err().unwrap_or_default() +} + +const TYPES: &str = "struct User {\n id: u64\n}\n\nstruct Post {\n id: u64\n}\n"; + +#[test] +fn test_a_bare_path_to_an_own_schema_is_an_error_with_a_fix_that_writes_package() { + use comline_core::schema::ir::compiler::import_resolver::BARE_OWN_SCHEMA_PATH; + + // Each form of a `use`, and what the fix makes of it. + let cases = [ + ("use types::User\n\nstruct R {\n u: User\n}\n", "use package::types::User\n\nstruct R {\n u: User\n}\n"), + ("use types\n\nstruct R {\n u: types::User\n}\n", "use package::types\n\nstruct R {\n u: types::User\n}\n"), + ("use types::*\n\nstruct R {\n u: User\n}\n", "use package::types::*\n\nstruct R {\n u: User\n}\n"), + ( + "use types::{User, Post}\n\nstruct R {\n u: User\n p: Post\n}\n", + "use package::types::{User, Post}\n\nstruct R {\n u: User\n p: Post\n}\n", + ), + ("use types::User as Account\n\nstruct R {\n u: Account\n}\n", "use package::types::User as Account\n\nstruct R {\n u: Account\n}\n"), + // Where the path sits in the file doesn't matter. + ( + "// the users\n\nuse types::User\n\nstruct R {\n u: User\n}\n", + "// the users\n\nuse package::types::User\n\nstruct R {\n u: User\n}\n", + ), + ]; + for (source, fixed) in cases { + let mut project = build_project(); + add_schema(&mut project, &["types"], TYPES); + add_schema(&mut project, &["api"], source); + + let errors = bare_path_errors(&project, &["api"]); + assert_eq!(errors.len(), 1, "{source}: {errors:?}"); + assert_eq!(errors[0].code, BARE_OWN_SCHEMA_PATH, "{source}"); + assert_eq!(errors[0].code.id, "import::bare-own-schema-path"); + let fix = errors[0].fix.as_ref().unwrap_or_else(|| panic!("{source}: no fix")); + assert_eq!(fix.apply(source), fixed, "{source}"); + + // The error stops the build, and what the fix writes builds, with the + // references the import brought in (`types::User`) as they were. + let error = interpret_context(&project).expect_err("a bare path to an own schema is refused").to_string(); + assert!(error.contains("import::bare-own-schema-path"), "{source}: {error}"); + let mut project = build_project(); + add_schema(&mut project, &["types"], TYPES); + add_schema(&mut project, &["api"], fixed); + assert!(bare_path_errors(&project, &["api"]).is_empty(), "{fixed}"); + interpret_context(&project).unwrap_or_else(|e| panic!("{fixed}: {e}")); + } +} + +#[test] +fn test_the_error_for_a_bare_path_says_what_to_write() { + let mut project = build_project(); + add_schema(&mut project, &["types"], TYPES); + add_schema(&mut project, &["api"], "use types::{User, Post}\n\nstruct R {\n u: User\n}\n"); + + let errors = bare_path_errors(&project, &["api"]); + assert_eq!(errors[0].message, "'types::{User, Post}' is one of this package's own schemas, reached by a bare path"); + assert!(errors[0].help.as_deref().unwrap().contains("write `package::types::{User, Post}`"), "{:?}", errors[0].help); + assert_eq!(errors[0].fix.as_ref().unwrap().title, "Write `package::types::{User, Post}`"); +} + +#[test] +fn test_a_bare_path_into_a_directory_of_the_package_gets_package_in_front() { + let mut project = build_project(); + add_schema(&mut project, &["shapes", "models"], TYPES); + add_schema(&mut project, &["shapes", "api"], "use shapes::models::User\n\nstruct R {\n u: User\n}\n"); + + let errors = bare_path_errors(&project, &["shapes", "api"]); + assert_eq!(errors.len(), 1, "{errors:?}"); + let source = "use shapes::models::User\n\nstruct R {\n u: User\n}\n"; + assert_eq!( + errors[0].fix.as_ref().unwrap().apply(source), + "use package::shapes::models::User\n\nstruct R {\n u: User\n}\n" + ); +} + +#[test] +fn test_relative_paths_to_own_schemas_are_not_errors() { + let mut project = build_project(); + add_schema(&mut project, &["shapes", "models"], TYPES); + add_schema(&mut project, &["shapes", "package_path"], "use package::shapes::models::User\n\nstruct A {\n u: User\n}\n"); + add_schema(&mut project, &["shapes", "parent_path"], "use parent::models::User\n\nstruct B {\n u: User\n}\n"); + add_schema(&mut project, &["shapes", "self_path", "models"], TYPES); + add_schema(&mut project, &["shapes", "self_path"], "use self::models::User\n\nstruct C {\n u: User\n}\n"); + add_schema(&mut project, &["shapes", "models", "glob"], "use parent::*\n\nstruct D {\n u: User\n}\n"); + + for namespace in ["package_path", "parent_path", "self_path"] { + let errors = bare_path_errors(&project, &["shapes", namespace]); + assert!(errors.is_empty(), "{namespace}: {errors:?}"); + } + let errors = bare_path_errors(&project, &["shapes", "models", "glob"]); + assert!(errors.is_empty(), "{errors:?}"); +} + +#[cfg(feature = "deps")] +#[test] +fn test_a_bare_path_to_a_dependency_or_std_is_not_this_error() { + let mut project = build_project_with_dependency(); + add_schema(&mut project, &["shared", "models"], "struct Thing {\n id: u64\n}\n"); + add_schema( + &mut project, + &["api"], + "use shared::models::Thing\nuse std::http::Request\n\nstruct R {\n t: Thing\n r: Request\n}\n", + ); + + let errors = bare_path_errors(&project, &["api"]); + assert!(errors.iter().all(|e| e.code.id != "import::bare-own-schema-path"), "{errors:?}"); +} + +#[test] +fn test_a_path_that_matches_nothing_is_unresolved_not_a_bare_own_path() { + let mut project = build_project(); + add_schema(&mut project, &["types"], TYPES); + add_schema(&mut project, &["api"], "use typse::User\n\nstruct R {\n u: User\n}\n"); + + let errors = bare_path_errors(&project, &["api"]); + assert_eq!(errors.len(), 1, "{errors:?}"); + assert_eq!(errors[0].code.id, "import::unresolved"); + assert!(errors[0].fix.is_none()); +} diff --git a/core/tests/spec/REV b/core/tests/spec/REV index e26093a..f96f2ef 100644 --- a/core/tests/spec/REV +++ b/core/tests/spec/REV @@ -1 +1 @@ -6d39fc363ab3dce9d1c4876b0d414edb375e25e9 +16dfe40d835b85b45de07131181339fe3e9f0ccb diff --git a/core/tests/spec/cases/bounded.json b/core/tests/spec/cases/bounded.json index 41da32b..b417922 100644 --- a/core/tests/spec/cases/bounded.json +++ b/core/tests/spec/cases/bounded.json @@ -264,7 +264,7 @@ }, { "path": "src/api.ids", - "text": "use models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n" + "text": "use package::models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n" } ], "dependencies": [] @@ -295,7 +295,7 @@ }, { "path": "src/api.ids", - "text": "use models::Blob\n\n@unverified = \"orders carry the upload\"\nprotocol Orders {\n function send(b: Blob);\n}\n" + "text": "use package::models::Blob\n\n@unverified = \"orders carry the upload\"\nprotocol Orders {\n function send(b: Blob);\n}\n" } ], "dependencies": [] @@ -354,7 +354,7 @@ }, { "path": "src/api.ids", - "text": "use models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n" + "text": "use package::models::Blob\n\nprotocol Orders {\n function send(b: Blob);\n}\n" } ], "dependencies": [] diff --git a/core/tests/spec/cases/imports.json b/core/tests/spec/cases/imports.json new file mode 100644 index 0000000..952f8d7 --- /dev/null +++ b/core/tests/spec/cases/imports.json @@ -0,0 +1,642 @@ +{ + "$schema": "../../specs/gen/compiler.schema.json", + "group": "imports", + "cases": [ + { + "id": "imports/bare-own-item", + "description": "a bare path to an item of the package's own schema is an error", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use models::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [ + { + "code": "import::bare-own-schema-path", + "severity": "error" + } + ] + } + }, + { + "id": "imports/bare-own-namespace", + "description": "a bare path to the package's own schema as a whole is an error", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use models\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [ + { + "code": "import::bare-own-schema-path", + "severity": "error" + } + ] + } + }, + { + "id": "imports/bare-own-multi", + "description": "a bare path with {...} to the package's own schema is an error", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use models::{Blob, Tag}\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [ + { + "code": "import::bare-own-schema-path", + "severity": "error" + } + ] + } + }, + { + "id": "imports/bare-own-glob", + "description": "a bare path with * to the package's own schema is an error", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use models::*\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [ + { + "code": "import::bare-own-schema-path", + "severity": "error" + } + ] + } + }, + { + "id": "imports/bare-own-nested", + "description": "a bare path to a schema in a directory of the package is an error, whatever the importing schema's own place", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/shapes/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/shapes/api.ids", + "text": "use shapes::models::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [ + { + "code": "import::bare-own-schema-path", + "severity": "error" + } + ] + } + }, + { + "id": "imports/package-item", + "description": "package:: reaches an item of the package's own schema", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use package::models::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/package-namespace", + "description": "package:: reaches the package's own schema as a whole", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use package::models\n\nstruct Upload {\n blob: models::Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/package-multi", + "description": "package:: with {...} reaches several items of the package's own schema", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use package::models::{Blob, Tag}\n\nstruct Upload {\n blob: Blob\n tag: Tag\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/package-glob", + "description": "package:: with * reaches every item of the package's own schema", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use package::models::*\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/package-nested", + "description": "package:: reaches a schema in a directory of the package from one beside it", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/shapes/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/shapes/api.ids", + "text": "use package::shapes::models::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/parent-item", + "description": "parent:: reaches a schema one level up from the importing schema's namespace", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/shapes/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/shapes/api.ids", + "text": "use parent::models::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/self-item", + "description": "self:: reaches a schema under the importing schema's own namespace", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "use self::models::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + }, + { + "path": "src/api/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/dependency-path", + "description": "a bare path whose first segment is a dependency reaches the dependency's schema", + "packages": [ + { + "name": "models", + "config": "congregation models\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [] + }, + { + "name": "app", + "config": "congregation app\nspecification_version = 1\ndependencies = {\n models = {\n path = \"../models\"\n }\n}\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "use models::api::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [ + { + "name": "models" + } + ] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/std-path", + "description": "a bare path whose first segment is std reaches the standard library's schema", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "use std::http::Request\n\nstruct Call {\n request: Request\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "protocols": [], + "builds": true, + "diagnostics": [] + } + }, + { + "id": "imports/unknown-first-segment", + "description": "a bare path that is neither a dependency, std nor one of the package's schemas is unresolved, not this error", + "packages": [ + { + "name": "app", + "config": "congregation app\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + }, + { + "path": "src/api.ids", + "text": "use modles::Blob\n\nstruct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [ + { + "code": "import::unresolved", + "severity": "error" + } + ] + } + }, + { + "id": "imports/own-schema-named-like-dependency", + "description": "a package's own schema named like one of its dependencies is refused, so a first segment is never both", + "packages": [ + { + "name": "models", + "config": "congregation models\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [] + }, + { + "name": "app", + "config": "congregation app\nspecification_version = 1\ndependencies = {\n models = {\n path = \"../models\"\n }\n}\n", + "schemas": [ + { + "path": "src/models.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [ + { + "name": "models" + } + ] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [], + "error_contains": [ + "schema 'models.ids' is named like the dependency 'models'" + ] + } + }, + { + "id": "imports/dependency-named-std", + "description": "std can't be a dependency's name", + "packages": [ + { + "name": "std", + "config": "congregation std\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [] + }, + { + "name": "app", + "config": "congregation app\nspecification_version = 1\ndependencies = {\n std = {\n path = \"../std\"\n }\n}\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [ + { + "name": "std" + } + ] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [], + "error_contains": [ + "reserved" + ] + } + }, + { + "id": "imports/dependency-named-package", + "description": "package can't be a dependency's name", + "packages": [ + { + "name": "package", + "config": "congregation package\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [] + }, + { + "name": "app", + "config": "congregation app\nspecification_version = 1\ndependencies = {\n package = {\n path = \"../package\"\n }\n}\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [ + { + "name": "package" + } + ] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [], + "error_contains": [ + "reserved" + ] + } + }, + { + "id": "imports/dependency-named-parent", + "description": "parent can't be a dependency's name", + "packages": [ + { + "name": "parent", + "config": "congregation parent\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [] + }, + { + "name": "app", + "config": "congregation app\nspecification_version = 1\ndependencies = {\n parent = {\n path = \"../parent\"\n }\n}\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [ + { + "name": "parent" + } + ] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [], + "error_contains": [ + "reserved" + ] + } + }, + { + "id": "imports/dependency-named-self", + "description": "self can't be a dependency's name", + "packages": [ + { + "name": "self", + "config": "congregation self\nspecification_version = 1\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Blob {\n size: u32\n}\n\nstruct Tag {\n id: u32\n}\n" + } + ], + "dependencies": [] + }, + { + "name": "app", + "config": "congregation app\nspecification_version = 1\ndependencies = {\n self = {\n path = \"../self\"\n }\n}\n", + "schemas": [ + { + "path": "src/api.ids", + "text": "struct Upload {\n blob: Blob\n}\n" + } + ], + "dependencies": [ + { + "name": "self" + } + ] + } + ], + "build": "app", + "expect": { + "builds": false, + "diagnostics": [], + "error_contains": [ + "reserved" + ] + } + } + ] +}