@@ -135,6 +135,8 @@ elabInstance info syn doc argDocs what fc cs n ps t expn ds = do
135135 [PClause fc iname lhs [] rhs wb]]
136136 iLOG (show idecls)
137137 mapM_ (rec_elabDecl info EAll info) idecls
138+ ist <- getIState
139+ checkInjectiveArgs fc n (class_determiners ci) (lookupTyExact iname (tt_ctxt ist))
138140 addIBC (IBCInstance intInst n iname)
139141
140142 where
@@ -276,3 +278,28 @@ elabInstance info syn doc argDocs what fc cs n ps t expn ds = do
276278 clauseFor m iname ns (PClauses _ _ m' _)
277279 = decorate ns iname m == decorate ns iname m'
278280 clauseFor m iname ns _ = False
281+
282+checkInjectiveArgs :: FC -> Name -> [Int] -> Maybe Type -> Idris ()
283+checkInjectiveArgs fc n ds Nothing = return ()
284+checkInjectiveArgs fc n ds (Just ty)
285+ = do ist <- getIState
286+ let (_, args) = unApply (instantiateRetTy ty)
287+ ci 0 ist args
288+ where
289+ ci i ist (a : as) | i `elem` ds
290+ = if isInj ist a then ci (i + 1) ist as
291+ else tclift $ tfail (At fc (InvalidTCArg n a))
292+ ci i ist (a : as) = ci (i + 1) ist as
293+ ci i ist [] = return ()
294+
295+ isInj i (P Bound n _) = True
296+ isInj i (P _ n _) = isConName n (tt_ctxt i)
297+ isInj i (App f a) = isInj i f && isInj i a
298+ isInj i (V _) = True
299+ isInj i (Bind n b sc) = isInj i sc
300+ isInj _ _ = True
301+
302+ instantiateRetTy (Bind n (Pi _ _ _) sc)
303+ = substV (P Bound n Erased) (instantiateRetTy sc)
304+ instantiateRetTy t = t
305+