Skip to content

Conversation

keyboardDrummer
Copy link
Member

@keyboardDrummer keyboardDrummer commented Jul 22, 2025

What was changed?

  • Allow initialized const fields that are non-init or possible-empty, in value traits. To ensure soundness, extra checks are added to members that inherit from such traits and that do not require constructors, such as datatypes and newtypes.

How has this been tested?

  • Updated test git-issue-1148.dfy with new cases

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@keyboardDrummer keyboardDrummer changed the title Turn off two errors Relax two errors Jul 22, 2025
@keyboardDrummer keyboardDrummer changed the title Relax two errors Relax restrictions for constants in value traits Jul 23, 2025
@keyboardDrummer keyboardDrummer marked this pull request as ready for review July 23, 2025 12:07
@keyboardDrummer
Copy link
Member Author

keyboardDrummer commented Jul 24, 2025

Actually I don't need this anymore, so putting this on hold.

Copy link
Member

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this restriction relaxing is sound, as it existed for reference traits before, and value traits are not that different in the sense that they still require a proper implementation. To my knowledge, we don't postulate anywhere the existence of values of a value trait.

@@ -1,4 +1,4 @@
// RUN: %testDafnyForEachResolver --expect-exit-code=2 "%s"
// RUN: %testDafnyForEachResolver --expect-exit-code=2 "%s" -- --general-traits=datatype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, could you use the most generic version so that you can also test for newtypes?

Suggested change
// RUN: %testDafnyForEachResolver --expect-exit-code=2 "%s" -- --general-traits=datatype
// RUN: %testDafnyForEachResolver --expect-exit-code=2 "%s" -- --general-traits=all

}

trait PossiblyDatatypeTrait {
static const StaticField: Y // error: Y is not nonempty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static const StaticField: Y // error: Y is not nonempty
static const StaticField: Y

Is the error appearing both in the static const declaration and the datatype, or just in the datatype? If the latter, this comment can be removed.

const InstanceField: Y
}

datatype PossiblyDatatypeTraitImpl extends PossiblyDatatypeTrait = PossiblyDatatypeTraitImpl { } // error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • example with newtype as well.

ghost const InstanceField: Y
}

datatype PossiblyDatatypeTraitImpl extends PossiblyDatatypeTrait = PossiblyDatatypeTraitImpl { } // error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an error here because InstanceField is not initialized?

git-issue-1148.dfy(80,16): Error: ghost const field 'Instance' of type 'Y' (which may be empty) must give a defining value
22 resolution/type errors detected in git-issue-1148.dfy
git-issue-1148.dfy(47,17): Error: static non-ghost const field 'StaticField' of type 'Y' (which does not have a default compiled value) must give a defining value
git-issue-1148.dfy(51,11): Error: datatype 'PossiblyDatatypeTraitImpl' must not inherit a non-ghost const field 'InstanceField' of type 'Y' (which does not have a default compiled value) without a defining value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a reformulation that gives two possible resolution outcomes?

Suggested change
git-issue-1148.dfy(51,11): Error: datatype 'PossiblyDatatypeTraitImpl' must not inherit a non-ghost const field 'InstanceField' of type 'Y' (which does not have a default compiled value) without a defining value
git-issue-1148.dfy(51,11): Error: const field 'InstanceField' of datatype 'PossiblyDatatypeTraitImpl' must either have a non-empty type or a defining value, and `Y` does not have a default compiled value

That way we have less "must not", "does not", and "without".

ghost const H: Nonempty := 15

const s: PossiblyEmpty // error: requires initialization
const s: PossiblyEmpty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's possibly the biggest potential issue but I think we are fine, since a trait is a collection of contracts, and someone should at least extend a mocked class that extends these contracts to test them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants