Instance matching treats unknown types as apart from every instance
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Start with types_match, match_instance, and the solver paths match_provided and match_compiler_instance in compiler-frontend/checking/src/core/constraint/matching.rs and constraint.rs. Read the existing matching and chain behavior, then add the two described compiler fixtures and capture their current output before changing matching. Run just t compiler; done means unknown types no longer hide known mismatches or trigger errors through a skipped chain member, with only expected diagnostics changed.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
Instance matching treats an unknown type as apart from every instance pattern, so the solver can't tell whether a failed match depends on the unknown type. Unknown types replace types whose errors were already reported, e.g., the invalid application in type Bad = Int String. As a result, the solver hides some instance errors that don't depend on the unknown type, and it reports some follow-up errors that do.
Independent instance errors are hidden
Since #646, the solver drops every unsolved constraint that mentions an unknown type and marks its evidence as an error without a report. A review comment on #646 pointed out that the rule also drops constraints that fail because of a known argument. For example, with only instance C Int Int, the constraint C Bad Boolean fails because Boolean can't match Int, whatever Bad means.
module Main where
class C :: Type -> Type -> Constraint
class C a b
instance C Int Int
type Bad = Int String
needsKnownMismatch :: C Bad Boolean => Int
needsKnownMismatch = 0
usesKnownMismatch :: Int
usesKnownMismatch = needsKnownMismatch
needsUnknownOnly :: C Bad Int => Int
needsUnknownOnly = 0
usesUnknownOnly :: Int
usesUnknownOnly = needsUnknownOnly
needsKnown :: C Int Boolean => Int
needsKnown = 0
usesKnown :: Int
usesKnown = needsKnown
viaArgument :: forall a. C a Boolean => a -> Int
viaArgument _ = 0
usesViaArgument :: Bad -> Int
usesViaArgument value = viaArgument value
I ran the fixture on main at 3284cb224 and on b487218a4, which is main just before #646 merged.
| Wanted constraint | Before #646 | main |
|---|---|---|
C Bad Boolean from a signature |
NoInstanceFound |
No error |
C Bad Boolean from instantiation |
NoInstanceFound for C (Int String) Boolean |
No error |
C Bad Int |
NoInstanceFound |
No error, as intended |
C Int Boolean |
NoInstanceFound |
NoInstanceFound |
The C Bad Boolean error doesn't depend on the unknown type, so main should still report it. main still reports the InvalidTypeApplication error on Bad, so the module fails to compile. The hidden error appears once Bad is fixed.
Instance chains skip members that might match
An unknown argument can also make an instance chain skip the member that the user meant, so the chain commits to a later member. The later member can then report an error that the unknown type caused.
module Main where
import Prim.TypeError (class Fail, Text)
class D :: Type -> Type -> Constraint
class D a b
instance D Int b
else instance Fail (Text "fell through to the catch-all") => D a b
type Bad = Int String
needsChain :: D Bad Boolean => Int
needsChain = 0
usesChain :: Int
usesChain = needsChain
main reports the following error after the InvalidTypeApplication error on Bad.
Error! · [CustomFailure] · Main.purs:18:13
•
│
• fell through to the catch-all
│
│ usesChain = needsChain
│ ╰─────────
•
With D Int Boolean in place of D Bad Boolean, the first member matches and the module reports no errors. b487218a4 reports the same CustomFailure, so the behavior predates #646. The guard from #646 can't catch the error, because the catch-all member solves the constraint and the guard only sees unsolved constraints.
Cause
types_match has no case for Type::Unknown, so an unknown type on either side falls through to (_, _) => Ok(MatchType::Apart). match_instance then returns Apart at the first apart argument that functional dependencies don't determine. For C Bad Boolean, it stops at Bad and never compares Boolean with Int.
The solver treats Apart as a reason to try the next chain member, which explains the chain behavior. After matching, the guard from #646 can only check whether the constraint contains an unknown type, because matching doesn't record why it failed.
Proposed direction
Fix the cause in matching instead of refining the guard. types_match could return a new result for unknown types, similar to MatchType::Stuck, meaning that the types might match but the solver can't commit to the instance. The solver would then do the following.
- Stop at a chain member that only an unknown type blocks.
- Report an unsolved constraint when every candidate is apart on known arguments.
- Drop an unsolved constraint without a report when an unknown type blocks one of its candidates.
The contains_unknown guard could then use the result that matching recorded, or be removed. The change also needs to cover the following cases.
- Given constraints in
match_provided. - Argument positions that functional dependencies determine.
- Classes that the compiler solves in
match_compiler_instance.
Also check how a reported constraint prints. Before #646, the instantiation case printed C (Int String) Boolean. Bad now expands to an unknown type, so the new message may show an unknown type placeholder instead.
Tests
Add both reproductions as compiler fixtures, and commit them with the current output first so that the fix commit shows the change in diagnostics. Then run just t compiler and confirm that only the expected diagnostics change.
Priority
The priority is low, because both effects only happen in modules that already report an error. Neither effect lets an invalid module compile.
Investigation: https://ampcode.com/threads/T-01a13033-7f9a-767c-aaae-c2eb0a8fa390
- Lingua principale
- Rust
- Stelle
- 118
- Fork
- 12
- Merge medio
- 7h 23m
- PR unite (30g)
- 100
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di purefunctor/purescript-iris
-
diagnostics enhancement language-server
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
purefunctor/purescript-iris#648 ·
I maintainer di solito rispondono entro 1 giorno
-
ideas
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
purefunctor/purescript-iris#620 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
purefunctor/purescript-iris#613 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
purefunctor/purescript-iris#560 ·
I maintainer di solito rispondono entro 1 giorno
-
tooling
Difficoltà 4/5 3-5 giorni Idoneità per principianti 58/100
purefunctor/purescript-iris#507 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di purefunctor/purescript-iris
Issue simili
-
Trash, bin or rubbish?Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 63/100
pop-os/cosmic-files#2117 ·
I maintainer di solito rispondono entro 2 giorni
-
L: java:gradle L: java:maven L: ruby:bundler T: bug 🐞
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
dependabot/dependabot-core#16547 ·
I maintainer di solito rispondono entro 1 giorno
-
bug status:ready-for-test
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
UniClipboard/UniClipboard#1985 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 75/100
RustCrypto/SSH#596 ·
-
area:breg rust
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
registrystack/registry-stack#2034 ·
I maintainer di solito rispondono entro 1 giorno