diff mbox

Fix ICE with spelling hints within explicit namespace aliases (PR c++/79298)

Message ID 1485893453-60841-1-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm Jan. 31, 2017, 8:10 p.m. UTC
PR c++/79298 reports a segfault when handling a qualified name lookup error,
where consider_binding_level dereferences a NULL cp_binding_level *.

The root cause is that suggest_alternative_in_explicit_scope assumes that
NAMESPACE_LEVEL (scope) is non-NULL, but this can be NULL for namespace aliases.

This patch adds a use of ORIGINAL_NAMESPACE to look through any namespace
aliases.

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.

OK for trunk?

gcc/cp/ChangeLog:
	PR c++/79298
	* name-lookup.c (suggest_alternative_in_explicit_scope): Resolve
	any namespace aliases.

gcc/testsuite/ChangeLog:
	PR c++/79298
	* g++.dg/spellcheck-pr79298.C: New test case.
---
 gcc/cp/name-lookup.c                      |  3 +++
 gcc/testsuite/g++.dg/spellcheck-pr79298.C | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/spellcheck-pr79298.C

Comments

Jason Merrill Jan. 31, 2017, 9:16 p.m. UTC | #1
OK.

On Tue, Jan 31, 2017 at 3:10 PM, David Malcolm <dmalcolm@redhat.com> wrote:
> PR c++/79298 reports a segfault when handling a qualified name lookup error,
> where consider_binding_level dereferences a NULL cp_binding_level *.
>
> The root cause is that suggest_alternative_in_explicit_scope assumes that
> NAMESPACE_LEVEL (scope) is non-NULL, but this can be NULL for namespace aliases.
>
> This patch adds a use of ORIGINAL_NAMESPACE to look through any namespace
> aliases.
>
> Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.
>
> OK for trunk?
>
> gcc/cp/ChangeLog:
>         PR c++/79298
>         * name-lookup.c (suggest_alternative_in_explicit_scope): Resolve
>         any namespace aliases.
>
> gcc/testsuite/ChangeLog:
>         PR c++/79298
>         * g++.dg/spellcheck-pr79298.C: New test case.
> ---
>  gcc/cp/name-lookup.c                      |  3 +++
>  gcc/testsuite/g++.dg/spellcheck-pr79298.C | 17 +++++++++++++++++
>  2 files changed, 20 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/spellcheck-pr79298.C
>
> diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
> index a3cb7ee..994f7f0 100644
> --- a/gcc/cp/name-lookup.c
> +++ b/gcc/cp/name-lookup.c
> @@ -4545,6 +4545,9 @@ bool
>  suggest_alternative_in_explicit_scope (location_t location, tree name,
>                                        tree scope)
>  {
> +  /* Resolve any namespace aliases.  */
> +  scope = ORIGINAL_NAMESPACE (scope);
> +
>    cp_binding_level *level = NAMESPACE_LEVEL (scope);
>
>    best_match <tree, tree> bm (name);
> diff --git a/gcc/testsuite/g++.dg/spellcheck-pr79298.C b/gcc/testsuite/g++.dg/spellcheck-pr79298.C
> new file mode 100644
> index 0000000..4d7bbf9
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/spellcheck-pr79298.C
> @@ -0,0 +1,17 @@
> +// Ensure that we can offer suggestions for misspellings via a
> +// namespace alias.
> +
> +namespace N { int x; int color; }
> +namespace M = N;
> +namespace O = M;
> +
> +int foo ()
> +{
> +  return M::y; // { dg-error ".y. is not a member of .M." }
> +}
> +
> +int bar ()
> +{
> +  return O::colour; // { dg-error ".colour. is not a member of .O." }
> +  // { dg-message "suggested alternative: .color." "" { target *-*-* } .-1 }
> +}
> --
> 1.8.5.3
>
diff mbox

Patch

diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index a3cb7ee..994f7f0 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -4545,6 +4545,9 @@  bool
 suggest_alternative_in_explicit_scope (location_t location, tree name,
 				       tree scope)
 {
+  /* Resolve any namespace aliases.  */
+  scope = ORIGINAL_NAMESPACE (scope);
+
   cp_binding_level *level = NAMESPACE_LEVEL (scope);
 
   best_match <tree, tree> bm (name);
diff --git a/gcc/testsuite/g++.dg/spellcheck-pr79298.C b/gcc/testsuite/g++.dg/spellcheck-pr79298.C
new file mode 100644
index 0000000..4d7bbf9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/spellcheck-pr79298.C
@@ -0,0 +1,17 @@ 
+// Ensure that we can offer suggestions for misspellings via a
+// namespace alias.
+
+namespace N { int x; int color; }
+namespace M = N; 
+namespace O = M; 
+
+int foo () 
+{
+  return M::y; // { dg-error ".y. is not a member of .M." }
+}
+
+int bar () 
+{
+  return O::colour; // { dg-error ".colour. is not a member of .O." }
+  // { dg-message "suggested alternative: .color." "" { target *-*-* } .-1 }
+}