Skip to content

Commit ce0417e

Browse files
dunhamsteveCodingCellist
authored andcommitted
[ warning ] Warn user when impossible clauses are ignored
1 parent f0165ec commit ce0417e

File tree

17 files changed

+183
-22
lines changed

17 files changed

+183
-22
lines changed

CHANGELOG_NEXT.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ should target this file (`CHANGELOG_NEXT`).
3737

3838
### Backend changes
3939

40+
### Compiler changes
41+
42+
* The compiler now warns the user when `impossible` clauses are ignored. This
43+
typically happens when a numeric literal or an ambiguous name appears in an
44+
`impossible` clause.
45+
4046
#### RefC Backend
4147

4248
* Fixed an issue to do with `alligned_alloc` not existing on older MacOS

libs/base/Data/Colist1.idr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ repeat v = v ::: repeat v
5454
||| Create a `Colist1` of `n` replications of the given element.
5555
public export
5656
replicate : (n : Nat) -> {auto 0 prf : IsSucc n} -> a -> Colist1 a
57-
replicate 0 _ impossible
5857
replicate (S k) x = x ::: replicate k x
5958

6059
||| Produce a `Colist1` by repeating a sequence
@@ -133,7 +132,6 @@ tail (_ ::: t) = t
133132
||| Take up to `n` elements from a `Colist1`
134133
public export
135134
take : (n : Nat) -> {auto 0 prf : IsSucc n} -> Colist1 a -> List1 a
136-
take 0 _ impossible
137135
take (S k) (x ::: xs) = x ::: take k xs
138136

139137
||| Take elements from a `Colist1` up to and including the

libs/base/Data/Nat.idr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ gt left right = lt right left
288288

289289
export
290290
lteReflectsLTE : (k : Nat) -> (n : Nat) -> lte k n === True -> k `LTE` n
291-
lteReflectsLTE (S k) 0 _ impossible
292291
lteReflectsLTE 0 0 _ = LTEZero
293292
lteReflectsLTE 0 (S k) _ = LTEZero
294293
lteReflectsLTE (S k) (S j) prf = LTESucc (lteReflectsLTE k j prf)
@@ -305,8 +304,6 @@ public export
305304
ltOpReflectsLT : (m,n : Nat) -> So (m < n) -> LT m n
306305
ltOpReflectsLT 0 (S k) prf = LTESucc LTEZero
307306
ltOpReflectsLT (S k) (S j) prf = LTESucc (ltOpReflectsLT k j prf)
308-
ltOpReflectsLT (S k) 0 prf impossible
309-
ltOpReflectsLT 0 0 prf impossible
310307

311308
export
312309
gtReflectsGT : (k : Nat) -> (n : Nat) -> gt k n === True -> k `GT` n

libs/base/Data/Nat/Order/Properties.idr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ minusLTE (S a) (S b) =
8383
||| Subtracting a positive number gives a strictly smaller number
8484
export
8585
minusPosLT : (a,b : Nat) -> 0 `LT` a -> a `LTE` b -> (b `minus` a) `LT` b
86-
minusPosLT 0 b z_lt_z a_lte_b impossible
87-
minusPosLT (S a) 0 z_lt_sa a_lte_b impossible
8886
minusPosLT (S a) (S b) z_lt_sa a_lte_b = LTESucc (minusLTE a b)
8987

9088
-- This is the opposite of the convention in `Data.Nat`, but 'monotone on the left' means the below

libs/contrib/Data/Fin/Extra.idr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ modFin : (n : Nat)
8080
-> (m : Nat)
8181
-> {auto mNZ : NonZero m}
8282
-> Fin m
83-
modFin n 0 impossible
8483
modFin 0 (S k) = FZ
8584
modFin (S j) (S k) =
8685
let n' : Nat

libs/contrib/Data/Nat/Order/Strict.idr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Decidable.Order.Strict
88

99
public export
1010
Irreflexive Nat LT where
11-
irreflexive {x = 0} _ impossible
1211
irreflexive {x = S _} (LTESucc prf) =
1312
irreflexive {rel = Nat.LT} prf
1413

libs/contrib/Data/Nat/Properties.idr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export
2222
multRightCancel : (a,b,r : Nat) -> (0 _ : NonZero r) -> a*r = b*r -> a = b
2323
multRightCancel a b 0 r_nz ar_eq_br = void (absurd r_nz)
2424
multRightCancel 0 0 r@(S predr) r_nz ar_eq_br = Refl
25-
multRightCancel 0 (S b) r@(S predr) r_nz ar_eq_br impossible
26-
multRightCancel (S a) 0 r@(S predr) r_nz ar_eq_br impossible
2725
multRightCancel (S a) (S b) r@(S predr) r_nz ar_eq_br =
2826
cong S $ multRightCancel a b r r_nz
2927
$ plusLeftCancel r (a*r) (b*r) ar_eq_br

libs/papers/Language/IntrinsicTyping/Krivine.idr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,6 @@ namespace Machine
614614
(trace (Element ctx (snd pctx)) sc (Element (arg :: env) (fst pctx, penv)) tr)
615615
trace (Element [] pctx) (Lam sc) env tr
616616
= case tr of
617-
Beta {arg = Element _ _, ctx = Element _ _} _ impossible
618617
Done sc .(fst env) => rewrite irrelevantUnit pctx in Done
619618

620619
public export

src/TTImp/Impossible.idr

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Data.List
1919
-- only guessing! But we can still do some type-directed disambiguation of
2020
-- names.
2121
-- Constants (fromInteger/fromString etc) won't be supported, because in general
22-
-- they involve resoling interfaces - they'll just become unmatchable patterns.
22+
-- they involve resolving interfaces - they'll just become unmatchable patterns.
2323

2424
match : {auto c : Ref Ctxt Defs} ->
2525
ClosedNF -> (Name, Int, ClosedTerm) -> Core Bool
@@ -57,11 +57,11 @@ nextVar fc
5757
put QVar (i + 1)
5858
pure (Ref fc Bound (MN "imp" i))
5959

60-
badClause : ClosedTerm -> List RawImp -> List RawImp -> List (Name, RawImp) -> Core a
60+
badClause : {auto c : Ref Ctxt Defs} -> ClosedTerm -> List RawImp -> List RawImp -> List (Name, RawImp) -> Core a
6161
badClause fn exps autos named
6262
= throw (GenericMsg (getLoc fn)
6363
("Badly formed impossible clause "
64-
++ show (fn, exps, autos, named)))
64+
++ show (!(toFullNames fn), exps, autos, named)))
6565

6666
mutual
6767
processArgs : {auto c : Ref Ctxt Defs} ->
@@ -115,6 +115,8 @@ mutual
115115
processArgs (App fc fn e') !(sc defs (toClosure defaultOpts Env.empty e'))
116116
exps [] named'
117117
processArgs fn ty [] [] [] = pure fn
118+
processArgs fn ty (x :: _) autos named
119+
= throw $ GenericMsg (getFC x) "Too many arguments"
118120
processArgs fn ty exps autos named
119121
= badClause fn exps autos named
120122

@@ -129,11 +131,15 @@ mutual
129131
= do defs <- get Ctxt
130132
prims <- getPrimitiveNames
131133
when (n `elem` prims) $
132-
throw (InternalError "Can't deal with constants here yet")
134+
throw (GenericMsg fc "Can't deal with \{show n} in impossible clauses yet")
133135

134136
gdefs <- lookupNameBy id n (gamma defs)
135-
[(n', i, gdef)] <- dropNoMatch !(traverseOpt (evalClosure defs) mty) gdefs
136-
| ts => ambiguousName fc n (map fst ts)
137+
mty' <- traverseOpt (evalClosure defs) mty
138+
[(n', i, gdef)] <- dropNoMatch mty' gdefs
139+
| [] => if length gdefs == 0
140+
then undefinedName fc n
141+
else throw $ GenericMsg fc "\{show n} does not match expected type"
142+
| ts => throw $ AmbiguousName fc (map fst ts)
137143
tynf <- nf defs Env.empty (type gdef)
138144
-- #899 we need to make sure that type & data constructors are marked
139145
-- as such so that the coverage checker actually uses the matches in
@@ -165,6 +171,9 @@ mutual
165171
mkTerm (IMustUnify fc r tm) mty exps autos named
166172
= Erased fc . Dotted <$> mkTerm tm mty exps autos named
167173
mkTerm (IPrimVal fc c) _ _ _ _ = pure (PrimVal fc c)
174+
-- We're taking UniqueDefault here, _and_ we're falling through to nextVar otherwise, which is sketchy.
175+
-- On option is to try each and emit an AmbiguousElab? We maybe should respect `UniqueDefault` if there
176+
-- is no evidence (mty), but we should _try_ to resolve here if there is an mty.
168177
mkTerm (IAlternative _ (UniqueDefault tm) _) mty exps autos named
169178
= mkTerm tm mty exps autos named
170179
mkTerm tm _ _ _ _ = nextVar (getFC tm)

src/TTImp/ProcessDef.idr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ processDef opts nest env fc n_in cs_in
11221122
log "declare.def.impossible" 3 $ "Generated impossible LHS: " ++ show lhsp
11231123
pure $ Just $ MkClause Env.empty lhsp (Erased (getFC rawlhs) Impossible))
11241124
(\e => do log "declare.def" 5 $ "Error in getClause " ++ show e
1125+
recordWarning $ GenericWarn (fromMaybe (getFC rawlhs) $ getErrorLoc e) (show e)
11251126
pure Nothing)
11261127
getClause (Right c) = pure (Just c)
11271128

0 commit comments

Comments
 (0)