Bug 16093 - Bad error messages for missing declarations.
Summary: Bad error messages for missing declarations.
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2004-06-20 10:56 UTC by Carlo Wood
Modified: 2023-10-12 13:17 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.1.0, 8.2.0, 9.0
Last reconfirmed: 2005-06-25 18:40:40


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Carlo Wood 2004-06-20 10:56:48 UTC
When a declaration is missing, the error returned by the compiler
is too confusing.  Consider the following short test case;

namespace n {
  int var;
  //class foo { };
  //template<typename T> class bar { };
}

n::var x1;
n::foo x3;
n::bar<int> x4;


The results in the following errors:

test.cc:7: error: `var' in namespace `n' does not name a type
test.cc:8: error: `foo' in namespace `n' does not name a type
test.cc:9: error: expected constructor, destructor, or type conversion before
'<' token
test.cc:9: error: expected `,' or `;' before '<' token

I'd like to suggest a different error for the case that there is
no declaration at all (this could be tested after it is already
detected that there is an error because foo is not a type):

test.cc:8: error: `foo' in namespace `n' is not declared

And of course, an improvement for templates.  For example:

test.cc:9: error: `bar' in namespace `n' is not declared
Comment 1 Carlo Wood 2004-06-20 11:09:13 UTC
On second thought, make that:

test.cc:8: error: `n::foo' has not been declared
test.cc:9: error: `n::bar` has not been declared

This is the same format that one gets when doing

using n::foo;

Note that the full namespace should be used - and not
what is literally in the source.  Consider:

namespace n1 {
  namespace n2 {
  }
}
namespace n3 { 
  namespace n2 {
    class foo { };
  }
}

using namespace n1;
n2::foo x;

Then one wants: error: `n1::n2::foo' has not been declared
Comment 2 Andrew Pinski 2004-06-25 22:58:22 UTC
Confirmed, there might be another bug with the same problem.
Comment 3 Andrew Pinski 2012-01-20 01:31:39 UTC
In all three cases on the trunk we get:
t.cc:7:1: error: ‘var’ in namespace ‘n’ does not name a type
t.cc:8:1: error: ‘foo’ in namespace ‘n’ does not name a type
t.cc:9:1: error: ‘bar’ in namespace ‘n’ does not name a type
Comment 4 Paolo Carlini 2013-07-05 22:30:23 UTC
In my opinion we are currently doing pretty well: the columns are ok; we handle templates with no excess error messages and we specifically talk about 'template type' for those. Comparing also to current clang, I think this bug could be closed.

16093.C:7:4: error: ‘var’ in namespace ‘n’ does not name a type
 n::var x1;
    ^
16093.C:8:4: error: ‘foo’ in namespace ‘n’ does not name a type
 n::foo x3;
    ^
16093.C:9:4: error: ‘bar’ in namespace ‘n’ does not name a template type
 n::bar<int> x4;
    ^
Comment 5 Manuel López-Ibáñez 2013-07-06 06:09:34 UTC
(In reply to Paolo Carlini from comment #4)
> In my opinion we are currently doing pretty well: the columns are ok; we
> handle templates with no excess error messages and we specifically talk
> about 'template type' for those. Comparing also to current clang, I think
> this bug could be closed.

It has improved a lot yes. But I think the point of the original reporter is that g++ should make a difference between the case where 'foo' has not been declared in the current context or the case where 'var' has been declared but it is not a type. This way G++ could do *better* than clang. ;-)

For the second testcase, we have also improved, since we print the full namespace, but clang has the helpful:

test.cc:27:5: error: no type named 'foo' in namespace 'n1::n2'; did you mean 'n3::n2::foo'?

But this is now a minor enhancement, not really a bug.
Comment 6 Paolo Carlini 2013-07-06 08:52:22 UTC
Honestly I didn't pay attention to the second testcase and didn't consider the first issue serious enough ;) Anyway, let's keep this open for now, thanks.
Comment 7 Manuel López-Ibáñez 2013-07-06 09:13:25 UTC
(In reply to Paolo Carlini from comment #6)
> Honestly I didn't pay attention to the second testcase and didn't consider
> the first issue serious enough ;) Anyway, let's keep this open for now,
> thanks.

The first issue is just a "nice-to-have" thing, but not very important. As for the second issue, it is already opened in a more general way at PR52277.
Comment 8 Martin Sebor 2019-02-03 21:17:41 UTC
No change since 2013.  I suspect that if this changes it will not be in response to this report but some other change so this might as well be resolved as good enough.