Skip to content

Commit a464c3a

Browse files
authored
Unrolled build for rust-lang#139075
Rollup merge of rust-lang#139075 - oli-obk:resolver-item-lifetime, r=compiler-errors Do not treat lifetimes from parent items as influencing child items ```rust struct A; impl Bar<'static> for A { const STATIC: &str = ""; // ^ no future incompat warning } ``` has no future incompat warning, because there is no ambiguity. But ```rust struct C; impl Bar<'_> for C { // ^^ this lifeimte const STATIC: &'static str = { struct B; impl Bar<'static> for B { const STATIC: &str = ""; // causes ^ to emit a future incompat warning } "" }; } ``` had one before this PR, because the impl for `B` (which is just a copy of `A`) thought it was influenced by a lifetime on the impl for `C`. I double checked all other `lifetime_ribs` iterations and all of them do check for `Item` boundaries. This feels very fragile tho, and ~~I think we should do not even be able to see ribs from parent items, but that's a different refactoring that I'd rather not do at the same time as a bugfix~~. EDIT: ah nevermind, this is needed for improving diagnostics like "use of undeclared lifetime" being "can't use generic parameters from outer item" instead. r? `@compiler-errors`
2 parents 19f42cb + dabee5d commit a464c3a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

compiler/rustc_resolve/src/late.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1833,14 +1833,17 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18331833
}
18341834
LifetimeRibKind::StaticIfNoLifetimeInScope { lint_id: node_id, emit_lint } => {
18351835
let mut lifetimes_in_scope = vec![];
1836-
for rib in &self.lifetime_ribs[..i] {
1836+
for rib in self.lifetime_ribs[..i].iter().rev() {
18371837
lifetimes_in_scope.extend(rib.bindings.iter().map(|(ident, _)| ident.span));
18381838
// Consider any anonymous lifetimes, too
18391839
if let LifetimeRibKind::AnonymousCreateParameter { binder, .. } = rib.kind
18401840
&& let Some(extra) = self.r.extra_lifetime_params_map.get(&binder)
18411841
{
18421842
lifetimes_in_scope.extend(extra.iter().map(|(ident, _, _)| ident.span));
18431843
}
1844+
if let LifetimeRibKind::Item = rib.kind {
1845+
break;
1846+
}
18441847
}
18451848
if lifetimes_in_scope.is_empty() {
18461849
self.record_lifetime_res(

tests/ui/consts/static-default-lifetime/static-trait-impl.rs

+13
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,17 @@ impl Bar<'static> for B {
1717
const STATIC: &str = "";
1818
}
1919

20+
struct C;
21+
impl Bar<'_> for C {
22+
// make ^^ not cause
23+
const STATIC: &'static str = {
24+
struct B;
25+
impl Bar<'static> for B {
26+
const STATIC: &str = "";
27+
// ^ to emit a future incompat warning
28+
}
29+
""
30+
};
31+
}
32+
2033
fn main() {}

0 commit comments

Comments
 (0)