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 TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Find Rust owners in the [compiler map](source-rust/README.md#where-code-belongs)

- **CANONICALIZE-SCALAR-RANGE-CONTRACTS.** Complete [scalar range contracts](wiki/spec/language/domains.md#declaration-and-membership).
- Prove field and case bounds at construction, on writes, on return of a field loan and after array updates.
- Check and lower scalar-field loans across case fields, recursive records/references, calls, joins, and [recasts](wiki/spec/layouts/recasts.md). Every shared scalar recast of a field or borrowed scalar is refused today, although [recasts.md](wiki/spec/layouts/recasts.md#shared-and-mutable-views) admits each (every `u8` representation is a valid `i8`): reading `let seen: @i8 = @cell.other as @i8; $seen` ("reference descriptor has no current origin account", also on [crash_entry_boolean_view/field](samples/cli/facts/crash_entry_boolean_view/field/main.omg) and `/local`), returning `@cell.other as @i8` from `machine view(cell: @Cell) -> @i8` ("call reference result has no complete contents inventory"), and returning `byte as @i8` for `byte: @u8` ("call dynamic returned view has an unsupported backing projection"). Returning `@cell.other` uncast runs.
- Check and lower scalar-field loans across case fields, recursive records/references, calls, joins, and proof-gated [recasts](wiki/spec/layouts/recasts.md) such as [crash_entry_boolean_view](samples/cli/facts/crash_entry_boolean_view/field/main.omg).
- Qualify migrated [range-contract customers](wiki/drafts/designs/range_suffix_migration.md) with their original results, including the filesystem programs under **FILESYSTEM-PLATFORM-SERVICES**.

- **STATIC-MACHINE-MIGRATION.** (covers **NAMED-IMPLEMENTATION-CONFORMANCE-MIGRATION**, **TRAIT-IMPLEMENTATION-SOURCE-MIGRATION**) Finish static machines, implementations, `dropped by` and [callback reach](wiki/drafts/designs/deferred_callback_reach.md).
Expand Down
45 changes: 45 additions & 0 deletions source-rust/application/tests/domain_access_field_observations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,48 @@ machine main() crashes Trap {
}
"#);
}

#[test]
fn shared_scalar_recasts_of_fields_and_borrowed_scalars_agree_natively_and_in_psi() {
program::assert_silent_return(
r#"data Cell [copy] { head: u8; other: u8; }
machine read_field(cell: @Cell) -> i8 {
let seen: @i8 = @cell.other as @i8;
$seen
}
machine view_field(cell: @Cell) -> @i8 {
@cell.other as @i8
}
machine view_byte(byte: @u8) -> @i8 {
byte as @i8
}
machine flag_byte(flag: @bool) -> u8 {
let seen: @u8 = flag as @u8;
$seen
}
machine main() crashes Trap {
let cell: Cell = Cell { head: 1u8, other: 255u8 };
let local_seen: @i8 = @cell.other as @i8;
let from_field: @i8 = view_field(@cell);
let byte: u8 = 254u8;
let from_byte: @i8 = view_byte(@byte);
let flag: bool = true;
let word_bits: u32 = 1065353216u32;
let as_float: @f32 = @word_bits as @f32;
let one: f32 = $as_float;
let back_bits: @u32 = @one as @u32;
transition read_field(@cell) == -1i8
&& $local_seen == -1i8
&& $from_field == -1i8
&& $from_byte == -2i8
&& flag_byte(@flag) == 1u8
&& $back_bits == 1065353216u32 {
true -> done(),
false -> wrong()
}
machine done() {}
machine wrong() { crash Trap; }
}
"#,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4295,9 +4295,16 @@ pub(crate) mod reference_input {
load_pointer |= placed.is_some_and(|backing| backing.resident_source().is_some());
let load_codec = storage.is_none() && semantic_scalar.is_some() && load_pointer;
let codec = match storage {
Some(storage) => storage
.stored_scalar
.and_then(runtime_abi::ScalarStorageCodec::from_scalar),
Some(storage) => storage.stored_scalar.and_then(|stored| {
// A same-width scalar representation view reinterprets the
// backing bits under the presented scalar's codec (for
// example, sign-extending `u8` bits read through `@i8`).
let presented = semantic_scalar.filter(|logical| {
terminal_semantics::reference_storage_byte_width(stored)
== terminal_semantics::reference_storage_byte_width(*logical)
});
runtime_abi::ScalarStorageCodec::from_scalar(presented.unwrap_or(stored))
}),
None if !load_codec && semantic_scalar.is_some() => {
projected_scalar_codec(projection_root, projection_path, declarations)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,38 @@ fn descriptor_location_with<'a, L: crate::StructuralDeclarations<'a>>(
let mut tip = Some(root_type);
let mut case_fields = None;
for segment in path {
if let StructuralPathSegment::RepresentationView {
structural_type,
stored_scalar,
} = segment
{
if case_fields.is_some()
|| tip.is_some_and(|root| {
types.unique(root).is_none_or(|declaration| {
matches!(
declaration.shape,
StructuralTypeShape::Opaque
| StructuralTypeShape::Reference { .. }
| StructuralTypeShape::AtomicCell(_)
)
})
})
{
return None;
}
tip = Some(
types
.representation_view(*structural_type, *stored_scalar)?
.id,
);
result.path.push(DescriptorPathSegment::RepresentationView {
structural_type: *structural_type,
stored_scalar: *stored_scalar,
});
continue;
}
let declaration = types.unique(tip?)?;
let canonical = match segment {
StructuralPathSegment::RepresentationView {
structural_type,
stored_scalar,
} if case_fields.is_none() => {
tip = Some(
types
.representation_view(*structural_type, *stored_scalar)?
.id,
);
DescriptorPathSegment::RepresentationView {
structural_type: *structural_type,
stored_scalar: *stored_scalar,
}
}
StructuralPathSegment::Referent if case_fields.is_none() => {
let StructuralTypeShape::Reference { referent, .. } = declaration.shape
else {
Expand Down Expand Up @@ -451,6 +467,37 @@ pub fn view_element_descriptor_prefix(
let mut case_fields = None;
let mut element = None;
for (position, segment) in path.iter().enumerate() {
if let DescriptorPathSegment::RepresentationView {
structural_type,
stored_scalar,
} = segment
{
if case_fields.is_some() {
return Err("a loan path has no exact typed projection");
}
if let Some(root) = tip {
let owner = declaration(types, root)
.ok_or("a loan path has no exact structural declaration")?;
if matches!(
owner.shape,
StructuralTypeShape::Opaque
| StructuralTypeShape::Reference { .. }
| StructuralTypeShape::AtomicCell(_)
) {
return Err("a loan path has no exact typed projection");
}
}
tip = Some(
crate::representation_view_declaration(
types.iter(),
*structural_type,
*stored_scalar,
)
.ok_or("a loan presentation has no exact declaration")?
.id,
);
continue;
}
let owner = declaration(types, tip.ok_or("a loan path crosses a scalar leaf")?)
.ok_or("a loan path has no exact structural declaration")?;
if case_fields.is_none()
Expand Down Expand Up @@ -510,18 +557,6 @@ pub fn view_element_descriptor_prefix(
_ => return Err("a loan index has no array or view owner"),
}
}
DescriptorPathSegment::RepresentationView {
structural_type,
stored_scalar,
} if case_fields.is_none() => Some(
crate::representation_view_declaration(
types.iter(),
*structural_type,
*stored_scalar,
)
.ok_or("a loan presentation has no exact declaration")?
.id,
),
// Extent observations name the descriptor's storage, not
// its separately borrowed bytes.
DescriptorPathSegment::Extent => return Ok(element),
Expand Down Expand Up @@ -572,22 +607,38 @@ fn resolved_path_with<'a, L: crate::StructuralDeclarations<'a>>(
let mut tip = Some(root_type);
let mut case_fields = None;
for segment in path {
if let DescriptorPathSegment::RepresentationView {
structural_type,
stored_scalar,
} = segment
{
if case_fields.is_some()
|| tip.is_some_and(|root| {
types.unique(root).is_none_or(|declaration| {
matches!(
declaration.shape,
StructuralTypeShape::Opaque
| StructuralTypeShape::Reference { .. }
| StructuralTypeShape::AtomicCell(_)
)
})
})
{
return None;
}
tip = Some(
types
.representation_view(*structural_type, *stored_scalar)?
.id,
);
result.push(StructuralPathSegment::RepresentationView {
structural_type: *structural_type,
stored_scalar: *stored_scalar,
});
continue;
}
let declaration = types.unique(tip?)?;
result.push(match segment {
DescriptorPathSegment::RepresentationView {
structural_type,
stored_scalar,
} if case_fields.is_none() => {
tip = Some(
types
.representation_view(*structural_type, *stored_scalar)?
.id,
);
StructuralPathSegment::RepresentationView {
structural_type: *structural_type,
stored_scalar: *stored_scalar,
}
}
DescriptorPathSegment::Referent if case_fields.is_none() => {
let StructuralTypeShape::Reference { referent, .. } = declaration.shape
else {
Expand Down Expand Up @@ -662,18 +713,34 @@ fn resolved_tip_with<'a, L: crate::StructuralDeclarations<'a>>(
let mut tip = Some(root_type);
let mut case_fields = None;
for (index, segment) in path.iter().enumerate() {
if let DescriptorPathSegment::RepresentationView {
structural_type,
stored_scalar,
} = segment
{
if case_fields.is_some()
|| tip.is_some_and(|root| {
types.unique(root).is_none_or(|declaration| {
matches!(
declaration.shape,
StructuralTypeShape::Opaque
| StructuralTypeShape::Reference { .. }
| StructuralTypeShape::AtomicCell(_)
)
})
})
{
return None;
}
tip = Some(
types
.representation_view(*structural_type, *stored_scalar)?
.id,
);
continue;
}
let declaration = types.unique(tip?)?;
match segment {
DescriptorPathSegment::RepresentationView {
structural_type,
stored_scalar,
} if case_fields.is_none() => {
tip = Some(
types
.representation_view(*structural_type, *stored_scalar)?
.id,
);
}
DescriptorPathSegment::Referent if case_fields.is_none() => {
let StructuralTypeShape::Reference { referent, .. } = declaration.shape else {
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ fn reference_sources_for_leaves(
| StructuralPathSegment::Case(_)
| StructuralPathSegment::FixedIndex(_)
| StructuralPathSegment::Referent
| StructuralPathSegment::RepresentationView { .. }
)
)
}) {
Expand Down Expand Up @@ -533,7 +534,8 @@ fn append_projection(
StructuralPathSegment::Field(_)
| StructuralPathSegment::Case(_)
| StructuralPathSegment::FixedIndex(_)
| StructuralPathSegment::Referent => {
| StructuralPathSegment::Referent
| StructuralPathSegment::RepresentationView { .. } => {
path.push(ReferenceOriginProjection::Exact(segment.clone()))
}
_ => return Err("dynamic returned view has an unsupported backing projection"),
Expand Down
Loading
Loading