Modelize type intersections
Add MIntersectionType to the model and use this new kind of type to enhance adaptative typing. As a side-effect, do some refactoring concerning the detection of nullables and primitive types.
For now, only essential (to avoid regressions) simplifications of type expressions are implemented.
Note: MTypeSet is designed to become the common superclass of MIntersectionType ( type intersections) and MUnionType (type unions) in future PRs.
This is quite nice. I think I need more time to review (and maybe discuss) the intersection API and implementation. However, I think the initial cleanup (and factorized maybenull) are useful alone. I suggests that you PR the cleanup part independently so it can be merged first (and faster). This will also simplify the review of the current PR.
♻️ Fix nitcs and nitce.
♻️ Take 3: Fix nitj.
I totally forgot that I had to compile that one separately. Change done on nitj and nitcs are now more similar. Sorry for the mess.
♻️ Add MIntersectionType::hash
@xymus I agree for the “TODOs” commit. But, for the 3 other commits that impact MType::intersection, the first is the basic definition, while the other are about moving code from typing. I could split everything in 3 PRs, but I thought it would be harder to review.
About the commits, I don't want you to change this PR. But I do have to insist that breaking up a single method across multiple commits does not help the review process. I tried to read commit by commit but the code was not coherent in each commit, there were useless variables and the doc described features not yet present. You must try to create commits with bigger commits encompassing larger features.
♻️
- [X] Reformat
SeparateCompilerVisitor::autobox - [X] Rework the documentation of
MType::can_be_null - [X] Rework the documentation of
MType::intersection - [X] Rework the documentation of
MTypeSet::mmodule - [X] Rework the documentation of
MTypeSet::operands - [X] Rework the documentation of
MTypeSet::separator - [X] Remove attribute
MIntersectionType::constraints- [X] Rename
MIntersectionType::from_constraintstofrom_operands
- [X] Rename
- [X] Move the by-operands cache to the module.
- [X] Reformat
MParameterType::resolve_for_anchored
♻️ Fix nitj
Fails seem to be related to unavailable ports on the test runner.
libevent warning: Opening localhost:18946 failed
:recycle:
- [x] Promote resolution logic to
MTypeSet. - [x] Order definitions in
MIntersectionTypeby name. - [x] Move cache to
MTypeso any caller ofintersectionbenefit from it and it no longer use a set as a key. - [x] Cache
hash, so retrieving an intersection of intersections from the cache is faster. - [x] Allow null anchors for
MFormalType::can_be_nullsonitmetricscan compile. - [x] Remove an unecessary redefiniton of
can_be_null.
Here are the Ir counts for (cd src && ./valgrind.sh -- ../bin/nitc -o /dev/null "$module"):
| Cache for intersections | Cache for hash |
nitc.nit1 |
../tests/alt/base_adaptive_full_alt2.nit |
|---|---|---|---|
| No | No | 10 628 637 825 | 53 062 955 |
| Yes | No | 10 624 738 666 | 52 798 002 |
| Yes | Yes | 10 618 383 914 | 52 798 022 |
Thank you @xymus for your precious feedback.
1 The current version of this PR.
♻️
- [x] Make
MTypeSet::apply_toredefinable only inmodel.
:recycle:
- [x] Only compute the depth of each operand once.
- [x] Add a to-do for ambiguous string representations.
- [x] Move most of the implementation of
undecoratetoMTypeSet(and add a to-do to not forgot to override it for unions). - [x] Move
hashnear==. - [x] Avoid infinite recursion in
as_notnull - [x] Add a test that provokes an infinite recursion if
intersectionandas_notnullare mutually recursive. - [x] Import only
core::kernelintest/base_adaptive_intersection_formal.nit - [x] Use
ModelDiamond. - [x] Remove
#alt2#for now allowed code intests/base_adaptive_full.nit - [x] Commit the implementation of
apply_toandas_notnullseparately from the rest ofMIntersectionTypeso the compiler is semantically valid at each commit. - [x] Flatten the intersections so we can compare them in the tests.
- [x] Allow any collection of types for
apply_to(no need to require a set).
Remaining discussion points:
- The
mmoduleattribute. - The signature of
apply_to. full_name- The implementation of
as_notnull. - Whether or not some changes should appear in the commit about
nitce.