PR c++/84993 identifies a problem with our suggestions for misspelled member names in the C++ FE for the case where the member is private. For example, given: class foo { public: double get_ratio() const { return m_ratio; } private: double m_ratio; }; void test(foo *ptr) { if (ptr->ratio >= 0.5) ;// etc } ...we currently emit this suggestion: <source>: In function 'void test(foo*)': <source>:12:12: error: 'class foo' has no member named 'ratio'; did you mean 'm_ratio'? if (ptr->ratio >= 0.5) ^~~~~ m_ratio ...but if the user follows this suggestion, they get: <source>: In function 'void test(foo*)': <source>:12:12: error: 'double foo::m_ratio' is private within this context if (ptr->m_ratio >= 0.5) ^~~~~~~ <source>:7:10: note: declared private here double m_ratio; ^~~~~~~ <source>:12:12: note: field 'double foo::m_ratio' can be accessed via 'double foo::get_ratio() const' if (ptr->m_ratio >= 0.5) ^~~~~~~ get_ratio() It feels wrong to be emitting a fix-it hint that doesn't compile, so this patch adds the accessor fix-it hint logic to this case, so that we directly offer a valid suggestion: <source>: In function 'void test(foo*)': <source>:12:12: error: 'class foo' has no member named 'ratio'; did you mean 'double foo::m_ratio'? (accessible via 'double foo::get_ratio() const') if (ptr->ratio >= 0.5) ^~~~~ get_ratio() gcc/cp/ChangeLog: PR c++/84993 * call.c (enforce_access): Move diagnostics to... (complain_about_access): ...this new function. * cp-tree.h (class access_failure_info): Rename split out field "m_field_decl" into "m_decl" and "m_diag_decl". (access_failure_info::record_access_failure): Add tree param. (access_failure_info::was_inaccessible_p): New accessor. (access_failure_info::get_decl): New accessor. (access_failure_info::get_diag_decl): New accessor. (access_failure_info::get_any_accessor): New member function. (access_failure_info::add_fixit_hint): New static member function. (complain_about_access): New decl. * typeck.c (access_failure_info::record_access_failure): Update for change to fields. (access_failure_info::maybe_suggest_accessor): Split out into... (access_failure_info::get_any_accessor): ...this new function... (access_failure_info::add_fixit_hint): ...and this new function. (finish_class_member_access_expr): Split out "has no member named" error-handling into... (complain_about_unrecognized_member): ...this new function, and check that the guessed name is accessible along the access path. Only provide a spell-correction fix-it hint if it is; otherwise, attempt to issue an accessor fix-it hint. gcc/testsuite/ChangeLog: PR c++/84993 * g++.dg/torture/accessor-fixits-9.C: New test. From-SVN: r265056
Name |
Last commit
|
Last update |
---|---|---|
.. | ||
ChangeLog | Loading commit data... | |
ChangeLog-1993 | Loading commit data... | |
ChangeLog-1994 | Loading commit data... | |
ChangeLog-1995 | Loading commit data... | |
ChangeLog-1996 | Loading commit data... | |
ChangeLog-1997 | Loading commit data... | |
ChangeLog-1998 | Loading commit data... | |
ChangeLog-1999 | Loading commit data... | |
ChangeLog-2000 | Loading commit data... | |
ChangeLog-2001 | Loading commit data... | |
ChangeLog-2002 | Loading commit data... | |
ChangeLog-2003 | Loading commit data... | |
ChangeLog-2004 | Loading commit data... | |
ChangeLog-2005 | Loading commit data... | |
ChangeLog-2006 | Loading commit data... | |
ChangeLog-2007 | Loading commit data... | |
ChangeLog-2008 | Loading commit data... | |
ChangeLog-2009 | Loading commit data... | |
ChangeLog-2010 | Loading commit data... | |
ChangeLog-2011 | Loading commit data... | |
ChangeLog-2012 | Loading commit data... | |
ChangeLog-2013 | Loading commit data... | |
ChangeLog-2014 | Loading commit data... | |
ChangeLog-2015 | Loading commit data... | |
ChangeLog-2016 | Loading commit data... | |
ChangeLog-2017 | Loading commit data... | |
ChangeLog.ptr | Loading commit data... | |
ChangeLog.tree-ssa | Loading commit data... | |
Make-lang.in | Loading commit data... | |
NEWS | Loading commit data... | |
call.c | Loading commit data... | |
cfns.gperf | Loading commit data... | |
cfns.h | Loading commit data... | |
class.c | Loading commit data... | |
config-lang.in | Loading commit data... | |
constexpr.c | Loading commit data... | |
constraint.cc | Loading commit data... | |
cp-gimplify.c | Loading commit data... | |
cp-lang.c | Loading commit data... | |
cp-objcp-common.c | Loading commit data... | |
cp-objcp-common.h | Loading commit data... | |
cp-tree.def | Loading commit data... | |
cp-tree.h | Loading commit data... | |
cp-ubsan.c | Loading commit data... | |
cvt.c | Loading commit data... | |
cxx-pretty-print.c | Loading commit data... | |
cxx-pretty-print.h | Loading commit data... | |
decl.c | Loading commit data... | |
decl.h | Loading commit data... | |
decl2.c | Loading commit data... | |
dump.c | Loading commit data... | |
error.c | Loading commit data... | |
except.c | Loading commit data... | |
expr.c | Loading commit data... | |
friend.c | Loading commit data... | |
g++spec.c | Loading commit data... | |
init.c | Loading commit data... | |
lambda.c | Loading commit data... | |
lang-specs.h | Loading commit data... | |
lex.c | Loading commit data... | |
logic.cc | Loading commit data... | |
mangle.c | Loading commit data... | |
method.c | Loading commit data... | |
name-lookup.c | Loading commit data... | |
name-lookup.h | Loading commit data... | |
operators.def | Loading commit data... | |
optimize.c | Loading commit data... | |
parser.c | Loading commit data... | |
parser.h | Loading commit data... | |
pt.c | Loading commit data... | |
ptree.c | Loading commit data... | |
repo.c | Loading commit data... | |
rtti.c | Loading commit data... | |
search.c | Loading commit data... | |
semantics.c | Loading commit data... | |
tree.c | Loading commit data... | |
type-utils.h | Loading commit data... | |
typeck.c | Loading commit data... | |
typeck2.c | Loading commit data... | |
vtable-class-hierarchy.c | Loading commit data... |