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 = "16dfe40d835b85b45de07131181339fe3e9f0ccb" }
comline-catalog = { git = "https://github.com/ComlineProject/specification", rev = "8a9d47dca94e34521bae37d664d032dd3654384b" }

# General
eyre = "0.6.8"
Expand Down
48 changes: 46 additions & 2 deletions core/src/package/config/ir/compiler/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::schema::ir::compiler::import_resolver::{
check_imports, resolve_use_to_schema, use_brings_into_scope, ImportResolver, ProjectImports,
};
use crate::schema::ir::compiler::interpreter::IncrementalInterpreter;
use crate::schema::ir::compiler::references::{check_references, Scope};
use crate::schema::ir::compiler::settings::enforcement::check_settings_enforcement;
use crate::schema::ir::compiler::settings::resolution::check_settings_conflicts;
use crate::schema::ir::compiler::settings::severity::resolve_diagnostic_overrides;
Expand Down Expand Up @@ -90,6 +91,11 @@ pub fn interpret_context(project_context: &ProjectContext) -> Result<()> {
let declarations = { schema_context.borrow().declarations.clone() };
let namespace = { schema_context.borrow().namespace.clone() };

// Every error of what the schema names is reported, not the first:
// the imports that don't resolve, and the names written that nothing
// brought into scope. A `use` that doesn't resolve still brings its
// name in, so the one never causes the other.
let mut unnamed = false;
if let Err(errors) = check_imports(&declarations, &namespace, project_context) {
let rendered: Vec<String> = errors
.iter()
Expand All @@ -100,6 +106,21 @@ pub fn interpret_context(project_context: &ProjectContext) -> Result<()> {
namespace.join("::"),
rendered.join("\n\n")
));
unnamed = true;
}
if let Err(errors) = check_references(&declarations, &namespace, project_context) {
let rendered: Vec<String> = errors
.iter()
.map(|error| schema_context.borrow().render(error))
.collect();
schema_errors.push(format!(
"Schema '{}' has names that aren't in scope:\n{}",
namespace.join("::"),
rendered.join("\n\n")
));
unnamed = true;
}
if unnamed {
continue;
}

Expand Down Expand Up @@ -468,18 +489,21 @@ fn imports_foreign(
});
};

// A qualified name may name its schema outright.
// A qualified name is a path from the package's root (core#290): it names
// its schema outright, and no `use` of the schema is asked about it.
let mut named_outright = false;
if let Some(qualifier) = qualifier {
let parts: Vec<String> = qualifier.split("::").map(str::to_string).collect();
if let Some((schema, remaining)) =
project_context.find_schema_by_import_namespace_parts(&parts)
{
if remaining.is_empty() {
declares(&schema, bare);
named_outright = true;
}
}
}
for decl in declarations {
for decl in declarations.iter().filter(|_| !named_outright) {
let Declaration::Use(use_stmt) = &decl.value else {
continue;
};
Expand Down Expand Up @@ -594,6 +618,26 @@ fn schema_imports(project_context: &ProjectContext) -> HashMap<String, HashSet<S
let from_namespace = schema_context.namespace_joined();
let mut depends_on = HashSet::new();

// What the schema writes in a type reaches a schema too, with no `use`
// for a full path (`package::billing::api::Invoice`): it is compiled
// after it, and the two can't import each other. A dependency's
// schemas arrive compiled, and are written against their own package.
if !schema_context.compile_state.borrow().complete {
let scope = Scope::new(
&schema_context.declarations,
&schema_context.namespace,
project_context,
);
for written in crate::schema::ir::compiler::references::named_types(&schema_context.declarations) {
if let Some(target) = scope.reaches(&written.name) {
let to_namespace = target.join("::");
if to_namespace != from_namespace {
depends_on.insert(to_namespace);
}
}
}
}

for decl in &schema_context.declarations {
if let Declaration::Use(use_stmt) = &decl.value {
let Ok(target) = resolve_use_to_schema(
Expand Down
22 changes: 20 additions & 2 deletions core/src/package/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::collections::HashMap;
// Crate Uses
use crate::schema::idl::grammar::Declaration;
use crate::schema::ir::compiler::import_resolver::ImportResolver;
use crate::schema::ir::compiler::references::named_types;

/// The namespace every std schema lives under.
pub const NAMESPACE: &str = "std";
Expand Down Expand Up @@ -65,7 +66,7 @@ fn std_imports(namespace: &[String], source: &str) -> Vec<Vec<String>> {
return vec![];
};
let resolver = ImportResolver::new(vec![], HashMap::new(), None);
document
let mut targets: Vec<Vec<String>> = document
.0
.iter()
.filter_map(|decl| match &decl.value {
Expand All @@ -74,7 +75,16 @@ fn std_imports(namespace: &[String], source: &str) -> Vec<Vec<String>> {
})
.map(|resolved| resolved.absolute_namespace)
.filter(|target| target.first().map(String::as_str) == Some(NAMESPACE))
.collect()
.collect();
// A full path in a type position needs no `use`: `std::http::Request`
// (core#290).
targets.extend(
named_types(&document.0)
.into_iter()
.filter(|written| written.name.starts_with("std::"))
.map(|written| written.name.split("::").map(String::from).collect::<Vec<String>>()),
);
targets
}

/// The schema a `use` of `target` resolves to, the way the build matches it:
Expand Down Expand Up @@ -112,6 +122,14 @@ mod tests {
);
}

#[test]
fn a_std_path_in_a_type_merges_its_schema_with_no_use() {
let sources = [schema(&["api"], "struct Call {\n request: std::http::Request\n}\n")];
assert_eq!(namespaces(&sources), ["std::http"]);
// A name that only looks like one isn't.
assert!(namespaces(&[schema(&["api"], "struct Call {\n request: stdlib::http::Request\n}\n")]).is_empty());
}

#[test]
fn an_unknown_std_path_merges_nothing() {
// `check_imports` then reports it, with a suggestion from std.
Expand Down
7 changes: 7 additions & 0 deletions core/src/schema/ir/compiler/alias_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::collections::{HashMap, HashSet};
use crate::package::config::ir::context::ProjectContext;
use crate::schema::idl::grammar::{Declaration, Type};
use crate::schema::ir::compiler::import_resolver::{find_schema_bringing_into_scope, schema_declares_symbol};
use crate::schema::ir::compiler::references::Scope;
use crate::schema::ir::validation::validator::is_primitive;
use crate::diagnostics::{Diagnostic, DiagnosticCode};

Expand Down Expand Up @@ -246,6 +247,12 @@ fn first_unresolvable_name(
if is_primitive(&bare) {
return None;
}
// A qualified name is read by the rule for them (core#290): a
// path from a name in scope, or from a root.
if let (true, Some((current_namespace, project_context))) = (full.contains("::"), use_context) {
let scope = Scope::new(declarations, current_namespace, project_context);
return (!scope.exists(&full)).then_some(full);
}
if alias_map.contains_key(&bare) {
return None;
}
Expand Down
117 changes: 106 additions & 11 deletions core/src/schema/ir/compiler/import_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,30 @@ pub struct ProjectImports<'a> {
}

impl ProjectImports<'_> {
/// The schema that declares what `name` names, and its name there. A
/// qualified type is frozen as its path from the package's root (core#290),
/// so it is the schema by that namespace; one that isn't (a validator
/// called `validators::StringBounds`, as written) and a bare name are read
/// through the schema's `use`s.
fn declaring_schema(&self, name: &str) -> Option<(Rc<RefCell<SchemaContext>>, String)> {
let bare = name.rsplit("::").next()?.to_string();
if let Some(schema) = self.by_path(name) {
return Some((schema, bare));
}
let schema =
find_schema_bringing_into_scope(&bare, self.declarations, self.namespace, self.project)?;
Some((schema, bare))
}

/// The schema whose namespace is the qualifier of `name`, when it
/// declares the name.
fn by_path(&self, name: &str) -> Option<Rc<RefCell<SchemaContext>>> {
let (qualifier, bare) = name.rsplit_once("::")?;
let parts: Vec<String> = qualifier.split("::").map(str::to_string).collect();
let (schema, remaining) = self.project.find_schema_by_import_namespace_parts(&parts)?;
let declared = remaining.is_empty() && importable_names(&schema.borrow()).iter().any(|n| n == bare);
declared.then_some(schema)
}
}

impl ImportLookup for ProjectImports<'_> {
Expand All @@ -609,6 +627,10 @@ impl ImportLookup for ProjectImports<'_> {
fn encodings(&self) -> crate::schema::ir::unverified::Encodings<'_> {
crate::schema::ir::unverified::Encodings::of(self.project)
}
fn reaches(&self, name: &str) -> bool {
self.by_path(name).is_some()
}

fn validator_targets(&self, name: &str) -> Option<Vec<TypeRef>> {
let (schema, bare) = self.declaring_schema(name)?;
let schema = schema.borrow();
Expand Down Expand Up @@ -796,7 +818,12 @@ pub fn declared_symbol_names(schema_context: &SchemaContext) -> Vec<String> {

/// Whether a `use` resolving to `resolved` (with `remaining` path segments
/// left over after the longest-prefix schema match, and an optional `as`
/// `alias`) brings the bare name `name` into scope. The branch order is
/// `alias`) brings the bare name `name` into scope. This is the older, wider
/// reading, where a whole-namespace `use` brings in every name the schema has:
/// the names a schema's `use`s are still read for (validators, constants, the
/// errors a function throws). A type's name is read by
/// [`references::Scope`](super::references::Scope), where it brings in its own
/// name only (core#290). The branch order is
/// load-bearing - glob, then explicit multi-import symbols, then a
/// whole-namespace `use`, then the remaining path/alias - and is the only
/// correct statement of this rule; shared by [`find_schema_bringing_into_scope`]
Expand Down Expand Up @@ -907,6 +934,26 @@ pub fn absolute_first_segment(use_path: &UsePath) -> Option<&str> {
(!matches!(first, "self" | "parent" | "package")).then_some(first)
}

/// Whether `parts` is a namespace that has no schema of its own: a
/// dependency's name or `std` on its own, or a directory of the project's
/// schemas (`billing`, with `billing::api` a schema).
pub(crate) fn is_namespace(
project_context: &ProjectContext,
dependencies: &HashSet<String>,
parts: &[String],
) -> bool {
if let [only] = parts {
if only == stdlib::NAMESPACE || dependencies.contains(only) {
return true;
}
}
!parts.is_empty()
&& project_context.schema_contexts.iter().any(|schema| {
let schema = schema.borrow();
schema.namespace.len() > parts.len() && schema.namespace.starts_with(parts)
})
}

/// A `use` path as it is written, without its alias.
fn written_path(use_path: &UsePath) -> String {
match use_path {
Expand Down Expand Up @@ -951,22 +998,31 @@ fn bare_own_schema_path(
.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) {
match own_path_fix(source, span, first, &format!("Write `{write}`"), "package::", 0) {
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> {
/// Replacing the `replaced` bytes where the `use` declaration at `span` names
/// `first`, the first segment of its path, with `text` (inserting, for 0, as
/// `package::` is put in front of a bare 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,
title: &str,
text: &str,
replaced: usize,
) -> 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::")
})
source
.get(at..)?
.starts_with(first)
.then(|| Fix::replace(title, (at, at + replaced), text))
}

/// Every `use` in `declarations` that doesn't resolve, as validation
Expand Down Expand Up @@ -1061,6 +1117,21 @@ pub fn check_imports(
continue;
}

// A namespace with no schema of its own brings its name into
// scope, to qualify with (core#290): a dependency's name or `std`
// on its own, or a directory of schemas (`use package::billing`).
// A directory reached by a bare path is the same error as a schema.
if target.resolved.symbols.is_empty()
&& is_namespace(project_context, &dependencies, &target.resolved.absolute_namespace)
{
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;
}

if first == stdlib::NAMESPACE {
errors.push(unresolved(unknown_std_path(&target.resolved.absolute_namespace)));
continue;
Expand All @@ -1070,13 +1141,37 @@ pub fn check_imports(
"no schema in this package or its dependencies matches '{}'",
target.resolved.absolute_namespace.join("::")
);
let mut fix = None;
if !top_level.contains(first) && !dependencies.contains(first) {
let known = top_level.iter().chain(&dependencies).map(String::as_str);
if let Some(suggestion) = closest(first, known) {
context.push_str(&format!(" - did you mean '{suggestion}'?"));
// One of the package's own schemas is reached through
// `package::`, so that is what is suggested (core#286); a
// dependency's name and `std` are absolute as they are.
let own = top_level.contains(&suggestion)
&& !dependencies.contains(&suggestion)
&& suggestion != stdlib::NAMESPACE
&& absolute_first_segment(&use_stmt.path) == Some(first);
if own {
let written = written_path(&use_stmt.path);
let corrected = format!("package::{suggestion}{}", &written[first.len()..]);
context.push_str(&format!(" - did you mean '{corrected}'?"));
fix = own_path_fix(
source.as_deref(),
decl.span,
first,
&format!("Write `{corrected}`"),
&format!("package::{suggestion}"),
first.len(),
);
} else {
context.push_str(&format!(" - did you mean '{suggestion}'?"));
}
}
}
errors.push(unresolved(context));
let mut diagnostic = unresolved(context);
diagnostic.fix = fix;
errors.push(diagnostic);
continue;
};

Expand Down
Loading
Loading