Repository navigation
feat: interfaces hold constants and operators, and enums declare operators - #174
Merged
Merged
Conversation
…ators An interface could hold only methods and get-only properties, and an enum no operators. Constants, default property bodies and operators in either were refused as not supported yet, so every class repeated them. Declare constants and operators on an interface: - An interface constant follows a class constant's rules and is public, read as Priced.CURRENCY, also through any class that implements it - An interface operator, written `static` alone, applies to every class that implements the interface, and a class's own operator for two of its values wins over it - The interface's trait of default bodies holds each operator twice: by its own name, which the engine's sortedBy and Set equality look up and a class's own operator replaces, and by the interface's name, which a call on two values of the interface runs on the class of the left one - Plain PHP gets the operator and the default property body through `use Priced\Defaults;`, and reads Priced::CURRENCY Give an interface property a default body, and no storage: - `string label => ...;` is the default body each class gets unless it overrides it - An initial value is an error, "An interface holds no storage, so it gives no initial value.", and an accessor body names the computed form - A law in an interface is an error, because a law is proved against the code of a class or an enum Order and compare an enum's cases by its own operators: - Size.Small < Size.Large compiles when Size declares `<=>`, and stays an error for an enum that declares none - An enum that declares `==` compares its cases by it instead of by value Zend/tests/sharp/ ├── InterfaceMembers/ │ ├── Priced.sharp <- added, an interface with a constant, a default body and <=> │ ├── Book.sharp <- added, a class that gets every default │ ├── Rare.sharp <- added, a class with its own label and <=> │ ├── Size.sharp <- added, an enum that declares <=> │ ├── Coin.sharp <- added, a data enum that declares == and hash() │ ├── Measure.sharp <- added, an interface with ==, + and unary - │ ├── Weight.sharp <- added, a class that gets Measure's operators │ └── Report.sharp <- added, the reads each row runs ├── interface_members.inc <- added, the rows and a plain PHP class using Priced\Defaults ├── interface_members.phpt <- added, runs the rows ├── interface_members_preload.inc <- added, preloads the fixture └── interface_members_preload.phpt <- added, runs the rows with the fixture preloaded docs/spec.md <- sections 19, 20 and 22 state the rules and run the examples sharp/harness/app/Priced.sharp <- added, the interface the section 22 class examples implement sharp/mago/crates/ ├── semantics/src/internal/checker/ │ ├── sharp.rs <- accepts interface constants and operators and enum operators, refuses storage and laws in an interface │ └── class_like/mod.rs <- leaves a PHP# interface constant's access modifier to the PHP# check ├── semantics/tests/sharp.rs <- the checker rows ├── codex/src/scanner/ │ ├── mod.rs <- copies an interface's operators into its trait of default bodies │ └── class_like.rs <- an enum that declares == gets no value equality ├── analyzer/src/ │ ├── expression/binary/utils.rs <- an enum that declares an operator takes it │ ├── expression/binary/comparison.rs <- orders the cases of an enum that declares <=> │ └── statement/class_like/method_signature.rs <- a class's own operator overrides no interface operator ├── analyzer/tests/sharp.rs <- the analyzer rows ├── sharp-bridge/src/ │ ├── lower.rs <- lowers interface constants and operators, enum operators, and the call on the left operand's class │ └── lower/types.rs <- names the class an operator runs on, and keeps operators out of property default bodies └── sharp-bridge/tests/lower.rs <- the lowering row
…bers #166 kept interface constants, interface property bodies and operators in an interface or an enum not supported yet, for this branch to decide. This branch decides them, so each now has one arm: the not-supported-yet arm keeps only the interface property forms still undecided, as `{ get; init; }`, and #166's refusal of a law in an interface is the one law arm. sharp/mago/crates/semantics/ ├── src/internal/checker/sharp.rs <- one arm per form: #166's law refusal stays, the undecided list narrows └── tests/sharp.rs <- #166's `abstract interface` error and refusal notes beside this branch's rows
patch-coverage found three paths no Rust test ran: `==` on two values of an interface, an enum with data that declares its own `==`, and an operator in a file whose first statement is an import. sharp/mago/crates/ ├── semantics/tests/sharp.rs <- the enum operator row starts with an import └── sharp-bridge/tests/lower.rs <- added row: interface `==` lifts null, and a data enum's own `==` replaces value equality
The checker and the bridge choose each operator use with one rule, as C# does: of the operators the two operand types, their parents and their interfaces declare, the most specific one that takes both runs. Two that take both where neither is more specific are one error at the use, which names the operator, both interfaces and the operator to declare instead. An interface operator is one static method of its Defaults trait, named after the interface, and each use calls it on the trait itself, so no class, trait collision or operand order changes which one runs. sortedBy passes the engine the operator < chooses for its key type, and a Set and a Map key objects by the most specific == their two classes and PHP# interfaces declare. A plain PHP class that implements a PHP# interface with default bodies without its Defaults trait is one error that names the trait. The ordering refusal on an enum says to declare operator <=>, an interface states hash() bare, an interface accessor body gets only PHP#'s message, and a bare constant that implements an interface's says it is written public const.
…bers
is_undecided keeps the branch's one interface arm, a property in another form
than { get; }, { get; set; } and => expr;, and drops the constant arms #173
retired.
Priced's constants read an index, a ? :, a match, a template with a cast, a case's name and a lambda, as a class constant's do since #173, in every mode and with preloading.
A Set or a Map chose an object's == on every hash and key match by building and
lowercasing two names per PHP# interface the object implements, and threw past
32 candidates. Each class's operators ==, with the class each parameter takes,
are now found the first time the request compares one of its objects and kept
until the request ends, in a list that grows with no limit.
Adding and finding 100,000 objects of a class that implements two PHP#
interfaces in a Set, debug builds, best of five:
master a70fc63 add 70.1 ms find 121.1 ms
before this commit add 332.0 ms find 508.1 ms
this commit add 77.7 ms find 133.2 ms
sharp_free_stamp becomes sharp_free_pointer, since both tables that use it hold
single allocations.
… members
is_undecided keeps the branch's one interface arm, a property in another form
than { get; }, { get; set; } and => expr;, beside #176's list, which moved break
with a level, |> and a partial application to unexpected_part.
A tie of two interfaces' operators is one error on each use that takes the class on both sides: <, +, sortedBy and contains. An operand of a class the codebase does not hold declares no operator. The arms no input reaches, a tie recomputed as no tie, a tie of one operator, a Defaults trait with no interface and a sortedBy with other than one type argument, are unreachable! with their reasons.
A Set or a Map of a class that two interfaces' operator == tie on was accepted, as was a Set of a struct that holds one, since a class that inherited any op_Equality counted as a value. Every == on two of them is refused, and at run time no single == takes them, so two equal ones never matched. The key check now asks the operator rule every == uses: a tied class is the tie error, and a struct that holds one can't be in a Set or key a Map. A test lowers that struct's generated ==, which has no operator to run on the tied property. A class the codebase does not hold declares no operator, in one iterator with the others.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An interface held only methods and get-only properties, and an enum no operators. Interface constants, interface property default bodies, and operators in an interface or an enum were refused with "This class member is not supported yet in PHP#.", so every class that implements an interface repeated them.
Solution
The block Jordan approved compiles and runs:
Priced.CURRENCYorBook.CURRENCY, and plain PHP asPriced::CURRENCY.staticalone, as every interface member is written bare. Every overloadable operator of section 19 may be declared, with the class rules:==needshash(), and<=>gives<,<=,>and>=.Priced\Defaultsholds each operator twice.op_Comparisonis the name the engine'ssortedByandSetequality look up, and a class's own operator replaces it.'op_Comparison@App\Priced'is the name no class replaces. A comparison of twoPricedvalues runs$a::{'op_Comparison@App\Priced'}($a, $b)on the class of the left operand, which runs once.==holds both operands and lifts null over them.<=>for two of its values wins. TwoRares order byRare's, and twoPricedvalues by the interface's.Size.Small < Size.Largecompiles only becauseSizedeclares<=>. A data enum that declares==compares its cases by it instead of by value.int count { get; } = 0;in an interface is "An interface holds no storage, so it gives no initial value.", and{ get => …; }names the computed formstring label => …;.{ get; init; }. fix: a PHP form PHP# never has is refused for good, with what PHP# writes instead #166's law refusal is the one law arm.Verification
cargo testonmago-semantics,mago-codex,mago-analyzerandmago-sharp-bridge: every target passes, with--test sharpat 428, 1036 and--test lowerat 450.cargo clippy -p mago-codex -p mago-semantics -p mago-analyzer -p mago-sharp-bridge --all-targets --all-features -- -D warnings: clean.sharp/bin/spec: "Checked 239 examples, ignored 1, and 0 fail.", with the newinterface-constants-and-operators,interface-members-in-classesandenum-operatorsexamples among them.sharp/bin/test --opcache --repeat --passesoninterface_members.phptandinterface_members_preload.phpt: both pass without opcache, with the file cache, with shared memory twice in one process, under each optimizer pass alone, and with the tracing and the function JIT.sharp/bin/test Zend/tests/sharp: 377 passed.