Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions core/examples/imports/src/hole.ids
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions core/oracle/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)));
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
126 changes: 121 additions & 5 deletions core/src/schema/ir/compiler/import_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -877,13 +892,95 @@ pub(crate) fn importable_names(schema_context: &SchemaContext) -> Vec<String> {
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<Fix> {
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
/// matched schema doesn't declare. Run by the real build (`interpret_context`)
/// 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.
Expand Down Expand Up @@ -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<String> = 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;
Expand Down Expand Up @@ -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<String> = if target.resolved.symbols == ["*"] {
vec![]
Expand Down
4 changes: 2 additions & 2 deletions core/tests/cas/dependency_vendoring_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
12 changes: 6 additions & 6 deletions core/tests/package/from_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -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`");
Expand All @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions core/tests/package/unverified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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!(
Expand All @@ -401,15 +401,15 @@ 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());
}

#[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()
Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion core/tests/package/unverified_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/tests/package/unverified_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion core/tests/package/unverified_strict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/tests/schema/ir/alias_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>::new());
Expand Down
Loading
Loading