diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 9654bf3fc01..4a21c0307f2 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -4812,9 +4812,13 @@ impl Resolver { DontAllowCapturingSelf) { Some(dl_def(def)) => return Some(def), _ => { - self.session.span_bug(span, - "self wasn't mapped to a \ - def?!") + if self.session.has_errors() { + // May happen inside a nested fn item, cf #6642. + return None; + } else { + self.session.span_bug(span, + "self wasn't mapped to a def?!") + } } } } diff --git a/src/test/compile-fail/issue-6642.rs b/src/test/compile-fail/issue-6642.rs new file mode 100644 index 00000000000..bffca995b8e --- /dev/null +++ b/src/test/compile-fail/issue-6642.rs @@ -0,0 +1,21 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct A; +impl A { + fn m(&self) { + fn x() { + self.m() + //~^ ERROR can't capture dynamic environment in a fn item + //~^^ ERROR `self` is not allowed in this context + } + } +} +fn main() {}