[cfe-dev] Feature Idea: Error Message Replacing

Douglas Gregor dgregor at apple.com
Tue Jun 9 11:26:25 PDT 2009


Hi Sebastian,

On Jun 6, 2009, at 5:50 AM, Sebastian Redl wrote:
> This is a feature idea I just came up with. I have no concrete plans  
> to
> implement this, but I thought I'd post it to the list to see if others
> like it as well, and to preserve it for future reference.
>
> I'm currently working on a Boost library, and in the compile errors  
> this
> shows up:
>
> error: invalid use of incomplete type ‘struct
> boost::property_tree::path_of<boost::any>’
>
> Because I've written this library, I know that this error really  
> means,
> "You haven't specialized the path_of struct for that template argument
> you're trying to use, but this is a prerequisite." If I were a user of
> the library, I'd have no clue.
> I think there should be a way for the library author to communicate  
> this
> knowledge to the user.

What do you need to do that static_assert doesn't already do?

> I'm imagining something like this:
>
> --- File: boost/property_tree/ptree.hpp (the library's main include)  
> ---
> #pragma clang error_catalog "boost/property_tree/property_tree.en.ec"
>
> // Users have to specialize this
> template <typename Key> struct path_of;
>
>
> --- File: boost/property_tree/property_tree.en.ec (pseudo-syntax) ---
> match "invalid use of incomplete type 'struct
> boost::property_tree::path_of<{T}>'"
> replacement "you need to specialize 'boost::property_tree::path_of'  
> for
> '$T' in order to use it as a property tree key"

Isn't this going to be brittle, since the wording of error messages  
might change from one Clang release to another?

> Then, when I do
>
> boost::basic_ptree<boost::any, int> pt;
>
> I get the error
>
> error: you need to specialize 'boost::property_tree::path_of' for
> 'boost::any' in order to use it as a property tree key

This is one cool thing that static_assert can't do , because you've  
captured the type via {T} and can now use that type in the reformatted  
error message. We could do the same kind of thing through an extension  
to static_assert:

   template<typename T>
   struct path_of {
     static_assert(always_false<T>::value, "you need to specialize  
'boost::property_tree::path_of' for %0 in order to use it as a  
property tree key", T);
   };

A couple years back, I prototyped something like this for GCC, using  
some gross hacks in the G++ front end and a poor approximation of  
static_assert. The results looked promising, because the custom  
diagnostic can give much better information than the generic error  
message, especially if it can refer to your own instantiation.

	- Doug




More information about the cfe-dev mailing list