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 @@ -24,7 +24,7 @@ comline-core-stdlib = { path = "../core_stdlib", version = "0.1.0" }
# 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 = "69d88327431275017ccce99fec43060467515a9f" }
comline-catalog = { git = "https://github.com/ComlineProject/specification", rev = "508f813c259afe07db20cf86bbe2f07028a4fbe0" }

# General
eyre = "0.6.8"
Expand Down
31 changes: 28 additions & 3 deletions core/src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ pub struct ProtocolSummary {
pub format: Choice,
pub framing: Choice,
pub handshake: Choice,
/// Whether it's marked `@unverified`.
/// Whether it gives something up: some guarantee isn't held, whether the
/// protocol's own `@unverified` or the package's `unverified` says why.
pub unverified: bool,
/// Every guarantee, in [`Guarantee::ALL`] order.
pub guarantees: Vec<GuaranteeRow>,
Expand Down Expand Up @@ -118,7 +119,8 @@ pub struct GuaranteeRow {
/// Empty when held.
pub causes: Vec<String>,
/// The `@unverified` reason, when it's given up and the marker carries
/// one: the plain form's, or the keyed form's for this guarantee.
/// one: the plain form's, or the keyed form's for this guarantee. The
/// package's, where it acknowledges the guarantee and the marker has none.
pub reason: Option<String>,
pub acceptance: Acceptance,
}
Expand Down Expand Up @@ -337,7 +339,10 @@ fn summarize(p: Protocol) -> ProtocolSummary {
format,
framing,
handshake,
unverified: unverified::marker(p.parameters).is_some(),
// What's given up, not who says why: a protocol only the package's
// `unverified` covers has no marker. `bounded_size` is in the frozen
// set alone.
unverified: !unverified::frozen_set(p.parameters).is_empty() || !causes.is_empty(),
guarantees,
}
}
Expand Down Expand Up @@ -603,4 +608,24 @@ mod tests {
assert_eq!(protocol(&context, "billing::api", "Billing"), Some(billing));
assert_eq!(protocol(&context, "api", "Billing"), None);
}

#[test]
fn a_protocol_only_the_package_covers_is_unverified_with_the_packages_reasons() {
let tmp = tempfile::tempdir().unwrap();
let app = tmp.path().join("app");
package(
&app,
"handshake = \"none\"\nunverified = {\n schema_agreement = \"pinned by contract\"\n \
encoding_agreement = \"one encoding only\"\n}\n\
settings = { annotations = { unverified = { allowed = true } } }\n",
"protocol Gateway {\n function ping();\n}\n",
);
let context = crate::package::build::compile_package(&app).unwrap();
let [gateway] = protocols(&context).try_into().unwrap();
// No marker of its own, and still unverified.
assert!(gateway.unverified);
assert_eq!(row(&gateway, Guarantee::SchemaAgreement).reason.as_deref(), Some("pinned by contract"));
assert_eq!(row(&gateway, Guarantee::EncodingAgreement).reason.as_deref(), Some("one encoding only"));
assert!(row(&gateway, Guarantee::VersionHonesty).held);
}
}
53 changes: 52 additions & 1 deletion core/src/package/config/ir/compiler/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ pub fn interpret_context(project_context: &ProjectContext) -> Result<()> {
.collect();
let wire = PackageWire::of(own_config, own_protocols.iter().map(Vec::as_slice));
check_package_encodings(project_context, own_format.as_deref(), &wire.framing, &wire.handshake)?;
// What the package says of what its own settings give up, for every
// protocol that takes them.
let package_unverified = crate::package::config::ir::frozen::unverified(own_config);
if let Some(package_unverified) = package_unverified {
check_package_unverified(package_unverified, &own_settings)?;
}
let mut package_covered = vec![];
let mut first_own: Option<Rc<RefCell<SchemaContext>>> = None;
// Package-level (`.idp`) settings origins aren't span-tracked yet
// (only schema-level `settings { ... }` blocks are) - a malformed
// package-level value falls back to usage-site-only diagnostics, same
Expand Down Expand Up @@ -136,11 +144,17 @@ pub fn interpret_context(project_context: &ProjectContext) -> Result<()> {
// Inject Namespace unit
let namespace_joined = schema_context.borrow().namespace_joined();
frozen_units.insert(0, FrozenUnit::Namespace(namespace_joined.clone()));
// What each protocol takes from the package is read before the
// package's handshake is frozen in, which makes it look its own.
let inherits = crate::schema::ir::unverified::inherits(&frozen_units);
wire.freeze_handshake(&mut frozen_units);
crate::schema::ir::unverified::freeze(
crate::schema::ir::unverified::freeze_with(
&mut frozen_units,
&crate::schema::ir::unverified::Encodings::of(project_context),
package_unverified,
&inherits,
);
package_covered.extend(crate::schema::ir::unverified::package_covered_by(&frozen_units));
// Settings gate what the schema's author wrote; the package's
// framing is the package's choice, so it's added after the snapshot
// enforcement checks.
Expand Down Expand Up @@ -248,6 +262,21 @@ pub fn interpret_context(project_context: &ProjectContext) -> Result<()> {

*schema_context.borrow().frozen_schema.borrow_mut() = Some(frozen_units);
schema_context.borrow().compile_state.borrow_mut().complete = true;
first_own.get_or_insert_with(|| schema_context.clone());
}

// The package's `unverified` lists what none of its protocols give up
// through its settings: a warning, on the first schema it compiled, as it
// has no schema of its own to be on.
if let (Some(package_unverified), Some(first), true) = (package_unverified, &first_own, schema_errors.is_empty()) {
package_covered.sort();
package_covered.dedup();
let unused = validation::validator::check_package_unverified_used(package_unverified, &package_covered);
let frozen = first.borrow().frozen_schema.borrow().clone().unwrap_or_default();
let (warnings, fatal) =
resolve_diagnostic_overrides(&frozen, unused, &own_settings, &package_settings_spans);
first.borrow().warnings.borrow_mut().extend(warnings);
schema_errors.extend(fatal.iter().map(|d| crate::diagnostics::render(d, "config.idp", "")));
}

if !schema_errors.is_empty() {
Expand Down Expand Up @@ -282,6 +311,28 @@ fn check_package_encodings(
Ok(())
}

/// The package's `unverified`, judged without its protocols: allowed by the
/// package's settings as `@unverified` is, and its keys and reasons sound.
fn check_package_unverified(
package_unverified: &crate::schema::ir::frozen::unit::ConfigValue,
package_settings: &crate::settings::SettingsDict,
) -> Result<()> {
let mut problems = crate::schema::ir::compiler::settings::enforcement::check_package_annotation(
"unverified",
package_settings,
&crate::settings::SettingsSpans::default(),
"config.idp's `unverified` is held to the same gate as `@unverified`",
)
.err()
.unwrap_or_default();
problems.extend(validation::validator::check_package_unverified(package_unverified));
if problems.is_empty() {
return Ok(());
}
let rendered: Vec<String> = problems.iter().map(|d| crate::diagnostics::render(d, "config.idp", "")).collect();
bail!("config.idp's `unverified` is invalid:\n{}", rendered.join("\n\n"))
}

/// Whether `name`, as a schema with these `declarations` uses it, is a
/// type another schema declares that holds a value without a maximum
/// ([`SchemaContext::unbounded`]). Every schema a `use` could bring it in
Expand Down
14 changes: 14 additions & 0 deletions core/src/package/config/ir/frozen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ pub enum FrozenUnit {
#[serde(default, skip_serializing_if = "Option::is_none")]
directions: Option<String>,
},
/// `unverified = ...`: why the package gives up what its own settings
/// give up, once for every protocol that takes them: a reason, or a
/// dictionary with one per guarantee, as `@unverified` takes. Frozen as
/// written, the keys unchecked; `schema::ir::unverified` and the compiler
/// judge it. Absent when the package says nothing.
Unverified(crate::schema::ir::frozen::unit::ConfigValue),
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -226,6 +232,14 @@ pub fn handshake_params(units: &[FrozenUnit]) -> BTreeMap<String, String> {
.unwrap_or_default()
}

/// The package's `unverified`, as written, if it says one.
pub fn unverified(units: &[FrozenUnit]) -> Option<&crate::schema::ir::frozen::unit::ConfigValue> {
units.iter().find_map(|unit| match unit {
FrozenUnit::Unverified(value) => Some(value),
_ => None,
})
}

/// The package's declared `wire` entries, `(format, framing)`, as written
/// (canonical). `None` for one it leaves out.
/// The package's `id`, if its config declares one.
Expand Down
32 changes: 32 additions & 0 deletions core/src/package/config/ir/interpreter/freezing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::package::config::ir::frozen::{
// External Uses
use comline_catalog::fact;

use crate::schema::ir::frozen::unit::ConfigValue;
use crate::schema::ir::compiler::settings::conformance::check_settings_conformance;

#[allow(unused)]
Expand Down Expand Up @@ -164,6 +165,7 @@ pub fn interpret_assignment(
}
}
}
"unverified" => vec![FrozenUnit::Unverified(interpret_assignment_unverified(&node.value)?)],
"wire" => {
let Value::Dictionary(items) = &node.value else {
return Err("'wire' should be a dictionary, like \
Expand Down Expand Up @@ -226,6 +228,36 @@ fn interpret_assignment_handshake(
Ok(units)
}

/// `unverified = "..."` or `unverified = { schema_agreement = "..." }`: frozen
/// as `@unverified` freezes, a reason or a dictionary of them. What the keys
/// and reasons say is judged where the guarantees are known, so an entry that
/// isn't a string is kept for that to report, not refused here.
fn interpret_assignment_unverified(value: &Value) -> Result<ConfigValue, Box<dyn snafu::Error>> {
use crate::package::config::dependency::strip_quotes;

match value {
Value::String(reason) => Ok(ConfigValue::Str(strip_quotes(&reason.value))),
Value::Dictionary(items) => {
let entries = items.assignments.iter().map(|assignment| {
let assignment = &assignment.value;
let value = match &assignment.value {
Value::String(reason) => ConfigValue::Str(strip_quotes(&reason.value)),
Value::Boolean(BooleanLiteral::True) => ConfigValue::Bool(true),
Value::Boolean(BooleanLiteral::False) => ConfigValue::Bool(false),
Value::Number(n) => n.value.parse().map(ConfigValue::Integer).unwrap_or(ConfigValue::Marker),
Value::Identifier(name) => ConfigValue::Name(name.value.clone()),
_ => ConfigValue::Marker,
};
(assignment.key.text(), value)
});
Ok(ConfigValue::Dict(entries.collect()))
}
_ => Err("'unverified' should be a reason, like \"the gateway pins schemas by contract\", or a \
dictionary with one for each guarantee, like { schema_agreement = \"...\" }"
.into()),
}
}

/// `wire = { format = "...", framing = "..." }`: both entries optional,
/// each folded onto its canonical name.
fn interpret_assignment_wire(
Expand Down
30 changes: 30 additions & 0 deletions core/src/package/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,36 @@ mod tests {
.expect("accepted for its whole set");
}

#[test]
fn the_acceptance_shows_the_reasons_the_package_gave() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("billing");
std::fs::create_dir_all(dir.join("src")).unwrap();
std::fs::write(
dir.join("config.idp"),
"congregation billing\nspecification_version = 1\nhandshake = \"none\"\n\
unverified = {\n schema_agreement = \"the gateway pins schemas by contract\"\n \
encoding_agreement = \"the gateway only speaks one encoding\"\n}\n\
settings = { annotations = { unverified = { allowed = true } } }\n",
)
.unwrap();
// No marker of its own: the package says why.
std::fs::write(dir.join("src/api.ids"), "protocol Billing {\n function charge();\n}\n").unwrap();
let cache = tmp.path().join("cache");

let err = resolve(&path_dep("billing", "billing", None), tmp.path(), &cache).map(|_| ()).unwrap_err().to_string();
assert!(err.contains("protocol `Billing` is unverified, giving up schema agreement and encoding agreement"), "{err}");
assert!(
err.contains(
"schema agreement \"the gateway pins schemas by contract\", \
encoding agreement \"the gateway only speaks one encoding\""
),
"{err}"
);
resolve(&accepting(&[Guarantee::SchemaAgreement, Guarantee::EncodingAgreement]), tmp.path(), &cache)
.expect("accepted for its whole set");
}

#[test]
fn a_consumer_judges_a_dependencys_schemas_by_the_dependencys_own_package() {
use crate::schema::ir::frozen::unit::FrozenUnit;
Expand Down
21 changes: 21 additions & 0 deletions core/src/schema/ir/compiler/settings/enforcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ pub fn check_settings_enforcement(
if errors.is_empty() { Ok(()) } else { Err(errors) }
}

/// A package-level use of an annotation's name, such as `config.idp`'s
/// `unverified`, held to the gate the annotation is under on a protocol:
/// what the package's settings say of `annotations.<name>.allowed`. `label`
/// says what's using it.
pub fn check_package_annotation(
name: &str,
package_settings: &SettingsDict,
package_spans: &SettingsSpans,
label: &str,
) -> Result<(), Vec<Diagnostic>> {
let mut errors = Vec::new();
check_governed(
keys::annotations_allowed(), package_settings, package_spans, DeclPath::Protocol, name,
label, None, &mut errors,
);
if errors.is_empty() { Ok(()) } else { Err(errors) }
}

fn check_field(
schema_effective: &SettingsDict,
schema_effective_spans: &SettingsSpans,
Expand Down Expand Up @@ -136,6 +154,9 @@ fn check_declaration(
match param {
// The preset selector itself, not a policy-governed feature.
FrozenUnit::Property { name, .. } if name == "settings" => {}
// Written by the compiler for what the package's `unverified`
// covers, which is gated where the package says it.
FrozenUnit::Property { name, .. } if name == crate::schema::ir::unverified::PACKAGE_KEY => {}
FrozenUnit::Property { name, .. } => {
check_governed(
keys::annotations_allowed(), &effective, &effective_spans, decl, name,
Expand Down
6 changes: 5 additions & 1 deletion core/src/schema/ir/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,11 @@ fn compare_parameters_with(
// A wire number: judged by `compare_id`, which knows the framing.
"id" => {}
// Adding or removing the marker is judged by the set it freezes,
// below; only a reworded reason is reported here.
// below; only a reworded reason is reported here. What the
// package's `unverified` says for the protocol (`unverified_package`)
// is reported whenever it changes, and as a patch: the reason
// moving between the package and the marker, or being reworded,
// changes nothing a peer sees, and what's given up is judged below.
"unverified" if old_value.is_none() || new_value.is_none() => {}
// A depth limit is a bound: raising it breaks old receivers,
// lowering it old senders.
Expand Down
Loading
Loading