Bug 77829 - Bad fix-it for nested-name-specifier
Summary: Bad fix-it for nested-name-specifier
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2016-10-03 16:27 UTC by Jonathan Wakely
Modified: 2023-09-21 17:29 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-10-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2016-10-03 16:27:57 UTC
namespace detail {
  int _foo(int i) { return i; }
}

void bar(int i) {
  detail::foo(i);
}

This says:

fixit.cc: In function ‘void bar(int)’:
fixit.cc:6:3: error: ‘foo’ is not a member of ‘detail’
   detail::foo(i);
   ^~~~~~
fixit.cc:6:3: note: suggested alternative: ‘bool’
   detail::foo(i);
   ^~~~~~
   bool


There are two problems here. Firstly, the range info for the rich location suggests replacing "detail" when it should be "foo". The patch generated by -fdiagnostics-generate-patch is:

--- fixit.cc
+++ fixit.cc
@@ -3,5 +3,5 @@
 }
 
 void bar(int i) {
-  detail::foo(i);
+  bool::foo(i);
 }


Secondly, detail::_foo should be a better match for detail::foo than detail::bool (which wouldn't be valid, because bool is not an identifier).
Comment 1 David Malcolm 2016-12-15 19:41:49 UTC
Has some similarities with PR c++/78656.
Comment 2 David Malcolm 2017-01-20 14:37:18 UTC
Author: dmalcolm
Date: Fri Jan 20 14:36:46 2017
New Revision: 244715

URL: https://gcc.gnu.org/viewcvs?rev=244715&root=gcc&view=rev
Log:
C++: fix fix-it hints for misspellings within explicit namespaces

gcc/cp/ChangeLog:
	PR c++/77829
	PR c++/78656
	* cp-tree.h (suggest_alternatives_for): Add bool param.
	(suggest_alternative_in_explicit_scope): New decl.
	* error.c (qualified_name_lookup_error): When SCOPE is a namespace
	that isn't the global one, call new function
	suggest_alternative_in_explicit_scope, only calling
	suggest_alternatives_for if it fails, and disabling near match
	searches fort that case.  When SCOPE is the global namespace,
	pass true for new param to suggest_alternatives_for to allow for
	fuzzy name lookups.
	* lex.c (unqualified_name_lookup_error): Pass true for new param
	to suggest_alternatives_for.
	* name-lookup.c (consider_binding_level): Add forward decl.
	(suggest_alternatives_for): Add "suggest_misspellings" param,
	using it to conditionalize the fuzzy name-lookup code.
	(suggest_alternative_in_explicit_scope): New function.
	* parser.c (cp_parser_primary_expression): When calling
	finish_id_expression, pass location of id_expression rather
	than that of id_expr_token.
	(cp_parser_id_expression): Convert local "unqualified_id" from
	tree to cp_expr to avoid implicitly dropping location information.

gcc/testsuite/ChangeLog:
	PR c++/77829
	PR c++/78656
	* g++.dg/spellcheck-pr77829.C: New test case.
	* g++.dg/spellcheck-pr78656.C: New test case.


Added:
    trunk/gcc/testsuite/g++.dg/spellcheck-pr77829.C
    trunk/gcc/testsuite/g++.dg/spellcheck-pr78656.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/error.c
    trunk/gcc/cp/lex.c
    trunk/gcc/cp/name-lookup.c
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 David Malcolm 2017-01-20 14:40:08 UTC
Should be fixed by r244715; marking as resolved.