back
▲ 3 points

Ask HN: Strictly-typed C Implementations?

by eatonphil·11y ago·4 comments·view on hn ↗
Is there a C compiler that does strict type-checking? For example, casting `char`s to `int`s is a compiler error or casting pointers of one type to another? Would this kind of compiler be as useful as I think it would for a certain class of errors?
4 comments
It shouldn't be hard to hack one from clang. You want to adjust usages of http://clang.llvm.org/doxygen/classclang_1_1CastExpr.html.

I would keep the main code as is, and add code to generate an error after every call that creates a cast (hacking that code into the constructors should work, too, but given the constructor for an empty cast and the subclasses, that will lead to lower quality error messages).

I expect you will want to allow some/all implicit casts, explicit ones from and to void pointers, and downcasts from 'pointer to struct with first field of type t' to 'pointer to t'.

But most importantly: what class of programming errors are you thinking of? That class of error, IMO, either requires a daft programmer or horrendous source code that is full of casts.

Not really trying to be a pedantic jerk, but...

Kind of by definition, what you would have would not be C, so there can't be any strictly-typed C implementations. I presume what you want is "just like C, except". And in fact, it's fine to want that. Just be aware that it really isn't C any more.

It would be a subset of C.
This sounds like something that static code analysis would handle. That said there would be corner cases where this would be a problem, for example a character literal is of type int, not of type char.