Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make datatype cycle detection independent of auto-init #4997

Merged
merged 24 commits into from
Jan 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5d45706
chore: Add documentation and sanity assertions
RustanLeino Jan 18, 2024
c868719
Compute IsObviouslyEmpty for inductive datatypes
RustanLeino Jan 18, 2024
d205272
Fix typo in comment
RustanLeino Jan 19, 2024
4036d59
Don’t use cache (that conflates all type-parrameter instantiations)
RustanLeino Jan 19, 2024
013546b
Also check cycles going through result type of total arrows
RustanLeino Jan 19, 2024
6fea52f
Add tests, and adjust for ordering for checks
RustanLeino Jan 19, 2024
a1f78c5
Simplify tests
RustanLeino Jan 19, 2024
bc9354b
Rename test file
RustanLeino Jan 19, 2024
98a3240
Add verification tests to confirm not auto-init
RustanLeino Jan 19, 2024
d6343ed
Set grounding-ctor-type-parameters and test compilation
RustanLeino Jan 19, 2024
8079558
Fix typo in comment
RustanLeino Jan 19, 2024
2fe447a
Merge branch 'master' into issue-4939
RustanLeino Jan 19, 2024
77f6046
Add release notes
RustanLeino Jan 19, 2024
2018422
Merge branch 'master' into issue-4939
RustanLeino Jan 19, 2024
5d1aec4
Merge branch 'master' into issue-4939
RustanLeino Jan 19, 2024
aec8edc
Fix whitespace
RustanLeino Jan 19, 2024
e660898
Don’t bother with cyclicity test if datatype had refinement error dur…
RustanLeino Jan 19, 2024
0cbc0dd
Change no-instance-because-of-datatype-cycle error into warning
RustanLeino Jan 22, 2024
a1275b1
Add a test that shows an empty datatype is provably empty
RustanLeino Jan 22, 2024
fc44f01
chore: Improve C#
RustanLeino Jan 23, 2024
2a660b5
fix: Box function-body results and let-RHS from datatype to trait
RustanLeino Jan 23, 2024
53ee5a4
Merge branch 'master' into issue-4939
RustanLeino Jan 23, 2024
84a81d5
Merge branch 'master' into issue-4939
ssomayyajula Jan 23, 2024
aad6805
Merge branch 'master' into issue-4939
RustanLeino Jan 24, 2024
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
Prev Previous commit
Next Next commit
Don’t use cache (that conflates all type-parrameter instantiations)
  • Loading branch information
RustanLeino committed Jan 19, 2024
commit 4036d591100cfe6569488890f68456cb05b84cf1
25 changes: 4 additions & 21 deletions Source/DafnyCore/Resolver/ModuleResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,9 +1273,8 @@ public partial class ModuleResolver {
}

// Perform the stratosphere check on inductive datatypes, and compute to what extent the inductive datatypes require equality support
var inductiveDatatypesCheckedForEmptiness = new Dictionary<IndDatatypeDecl, bool>();
foreach (var dtd in declarations.ConvertAll(decl => decl as IndDatatypeDecl).Where(dtd => dtd != null)) {
if (IsObviouslyEmpty(dtd, inductiveDatatypesCheckedForEmptiness)) {
if (AreThereAnyObviousSignsOfEmptiness(UserDefinedType.FromTopLevelDecl(dtd.tok, dtd), new HashSet<IndDatatypeDecl>())) {
reporter.Error(MessageSource.Resolver, dtd,
$"because of cyclic dependencies among constructor argument types, no instances of datatype '{dtd.Name}' can be constructed");
}
Expand Down Expand Up @@ -2853,30 +2852,14 @@ public ReportOtherAdditionalInformation_Visitor(ModuleResolver resolver)
}
}

/// <summary>
/// Determine if "datatypeDecl" is involved in a cycle in such a way that it is obvious that the type has no instances.
/// Return the answer.
///
/// "informationSoFar" is used as a cache of computed information. The method updates the cache for "datatypeDecl".
/// </summary>
bool IsObviouslyEmpty(IndDatatypeDecl datatypeDecl, IDictionary<IndDatatypeDecl, bool> informationSoFar) {
var datatype = UserDefinedType.FromTopLevelDecl(datatypeDecl.tok, datatypeDecl);
var isEmpty = AreThereAnyObviousSignsOfEmptiness(datatype, informationSoFar, new HashSet<IndDatatypeDecl>());
informationSoFar.Add(datatypeDecl, isEmpty);
return isEmpty;
}

private bool AreThereAnyObviousSignsOfEmptiness(Type type, IDictionary<IndDatatypeDecl, bool> informationSoFar, ISet<IndDatatypeDecl> beingVisited) {
private bool AreThereAnyObviousSignsOfEmptiness(Type type, ISet<IndDatatypeDecl> beingVisited) {
type = type.NormalizeExpand(); // cut through type proxies, type synonyms, but being mindful of what's in scope
if (type is UserDefinedType { ResolvedClass: var cl} udt) {
Contract.Assert(cl != null);
if (cl is NewtypeDecl newtypeDecl) {
return AreThereAnyObviousSignsOfEmptiness(newtypeDecl.RhsWithArgument(udt.TypeArgs), informationSoFar, beingVisited);
return AreThereAnyObviousSignsOfEmptiness(newtypeDecl.RhsWithArgument(udt.TypeArgs), beingVisited);
}
if (cl is IndDatatypeDecl datatypeDecl) {
if (informationSoFar.TryGetValue(datatypeDecl, out var isObviouslyEmpty)) {
return isObviouslyEmpty;
}
if (beingVisited.Contains(datatypeDecl)) {
// This datatype may be empty, but it's definitely empty if we consider only the constructors that have been visited
// since AreThereAnyObviousSignsOfEmptiness was called from IsObviouslyEmpty.
Expand All @@ -2885,7 +2868,7 @@ public ReportOtherAdditionalInformation_Visitor(ModuleResolver resolver)
beingVisited.Add(datatypeDecl);
var typeMap = TypeParameter.SubstitutionMap(datatypeDecl.TypeArgs, udt.TypeArgs);
var isEmpty = datatypeDecl.Ctors.TrueForAll(ctor =>
ctor.Formals.Exists(formal => AreThereAnyObviousSignsOfEmptiness(formal.Type.Subst(typeMap), informationSoFar, beingVisited)));
ctor.Formals.Exists(formal => AreThereAnyObviousSignsOfEmptiness(formal.Type.Subst(typeMap), beingVisited)));
beingVisited.Remove(datatypeDecl);
return isEmpty;
}
Expand Down