Bug 83056 - GCC suggests the use of previously reported undeclared identifiers when reporting new undeclared identifiers
Summary: GCC suggests the use of previously reported undeclared identifiers when repor...
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.2.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-11-20 01:04 UTC by James Almer
Modified: 2017-11-21 22:08 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-11-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description James Almer 2017-11-20 01:04:07 UTC
$ cat suggest.c
enum {
    TYPE_A,
}

int fn(void)
{
    int b = TYPE_B;
    int c = TYPE_C;
    int d = TYPE_D;
    return 0;
}

$ gcc -O3 -Wall -c suggest.c
suggest.c: In function 'fn':
suggest.c:7:13: error: 'TYPE_B' undeclared (first use in this function); did you mean 'TYPE_A'?
     int b = TYPE_B;
             ^~~~~~
             TYPE_A
suggest.c:7:13: note: each undeclared identifier is reported only once for each function it appears in
suggest.c:8:13: error: 'TYPE_C' undeclared (first use in this function); did you mean 'TYPE_B'?
     int c = TYPE_C;
             ^~~~~~
             TYPE_B
suggest.c:9:13: error: 'TYPE_D' undeclared (first use in this function); did you mean 'TYPE_C'?
     int d = TYPE_D;
             ^~~~~~
             TYPE_C

For some reason, for every new undeclared identifier gcc suggest to use a previously reported (but apparently similar) undeclared identifier.
Shouldn't it always suggest TYPE_A, the only defined identifier?
Comment 1 David Malcolm 2017-11-20 15:18:57 UTC
Thanks for filing this.

Confirmed: this still affects trunk, with the C frontend (the "-O3 -Wall" aren't needed).  The C++ frontend doesn't seem to be affected.
Comment 2 David Malcolm 2017-11-21 15:45:46 UTC
Candidate patch:
  https://gcc.gnu.org/ml/gcc-patches/2017-11/msg01907.html
Comment 3 David Malcolm 2017-11-21 22:00:24 UTC
Author: dmalcolm
Date: Tue Nov 21 21:59:53 2017
New Revision: 255038

URL: https://gcc.gnu.org/viewcvs?rev=255038&root=gcc&view=rev
Log:
C: don't suggest names that came from earlier failures (PR c/83056)

PR c/83056 reports an issue affecting trunk and gcc-7 in which
the C frontend's implementation of lookup_name_fuzzy uses undeclared
identifiers as suggestions when encountering subsequent undeclared
identifiers.

The fix is to filter out the names bound to error_mark_node
in lookup_name_fuzzy.

The C++ frontend is unaffected, as it already does this.

gcc/c/ChangeLog:
	PR c/83056
	* c-decl.c (lookup_name_fuzzy): Don't suggest names that came from
	earlier failed lookups.

gcc/testsuite/ChangeLog:
	PR c/83056
	* gcc.dg/spellcheck-pr83056.c: New test case.


Added:
    trunk/gcc/testsuite/gcc.dg/spellcheck-pr83056.c
Modified:
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-decl.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 David Malcolm 2017-11-21 22:08:10 UTC
Fixed on trunk for gcc 8 by r255038; not yet fixed on gcc-7-branch.