https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108238
--- Comment #2 from Barnabás Pőcze <pobrn at protonmail dot com> --- Here is a change that I believe might address this. It seems to work but I have never done anything in gcc, so probably has shortcomings. The error points to the return expression, like this: asd2.cpp: In function ‘auto f2()’: asd2.cpp:9:20: error: ‘returns_nonnull’ attribute on a function not returning a pointer 9 | auto f2() { return 42; } | ^~ --- diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 4667f6de311..50f5ad6b8f1 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -5761,8 +5761,9 @@ static tree handle_returns_nonnull_attribute (tree *node, tree name, tree, int, bool *no_add_attrs) { + auto type_code = TREE_CODE (TREE_TYPE (*node)); // Even without a prototype we still have a return type we can check. - if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE) + if (type_code != POINTER_TYPE && type_code != TEMPLATE_TYPE_PARM) { error ("%qE attribute on a function not returning a pointer", name); *no_add_attrs = true; diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 1656d02d6d1..b3ae608b350 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12305,6 +12305,10 @@ apply_deduced_return_type (tree fco, tree return_type) if (return_type == error_mark_node) return; + tree returns_nonnull = lookup_attribute("returns_nonnull", TYPE_ATTRIBUTES (TREE_TYPE (fco))); + if (returns_nonnull && TREE_CODE (return_type) != POINTER_TYPE) + error ("%<returns_nonnull%> attribute on a function not returning a pointer"); + if (DECL_CONV_FN_P (fco)) DECL_NAME (fco) = make_conv_op_name (return_type);