From c7b31d0ac983fdf7693a0029fdb0e16247c6599d Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Fri, 15 Jul 2022 12:22:36 -0500 Subject: [PATCH] Correctly handle path stability for 'use tree' items PR #5956 started checking the stability of path segments. However, this was not applied to 'use tree' items (e.g. 'use some::path::{ItemOne, ItemTwo}') due to the way that we desugar these items in HIR lowering. This PR modifies 'use tree' lowering to preserve resolution information, which is needed by stability checking. --- compiler/rustc_ast_lowering/src/item.rs | 12 +++++++++-- compiler/rustc_ast_lowering/src/lib.rs | 10 ++++++++++ src/test/ui/lint/lint-output-format.rs | 1 + src/test/ui/lint/lint-output-format.stderr | 12 +++++++++-- .../stability-attribute/stable-in-unstable.rs | 8 ++++++++ .../stable-in-unstable.stderr | 20 ++++++++++++++++++- 6 files changed, 58 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 7da49143b461e..e176364c494ea 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -533,7 +533,11 @@ impl<'hir> LoweringContext<'_, 'hir> { let ident = *ident; let mut path = path.clone(); for seg in &mut path.segments { - seg.id = self.next_node_id(); + // Give the cloned segment the same resolution information + // as the old one (this is needed for stability checking). + let new_id = self.next_node_id(); + self.resolver.clone_res(seg.id, new_id); + seg.id = new_id; } let span = path.span; @@ -602,7 +606,11 @@ impl<'hir> LoweringContext<'_, 'hir> { // Give the segments new node-ids since they are being cloned. for seg in &mut prefix.segments { - seg.id = self.next_node_id(); + // Give the cloned segment the same resolution information + // as the old one (this is needed for stability checking). + let new_id = self.next_node_id(); + self.resolver.clone_res(seg.id, new_id); + seg.id = new_id; } // Each `use` import is an item and thus are owners of the diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index b79c1cafba008..ad6b857e94d43 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -155,6 +155,10 @@ trait ResolverAstLoweringExt { fn legacy_const_generic_args(&self, expr: &Expr) -> Option>; fn get_partial_res(&self, id: NodeId) -> Option; fn get_import_res(&self, id: NodeId) -> PerNS>>; + // Clones the resolution (if any) on 'source' and applies it + // to 'target'. Used when desugaring a `UseTreeKind::Nested` to + // multiple `UseTreeKind::Simple`s + fn clone_res(&mut self, source: NodeId, target: NodeId); fn get_label_res(&self, id: NodeId) -> Option; fn get_lifetime_res(&self, id: NodeId) -> Option; fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>; @@ -192,6 +196,12 @@ impl ResolverAstLoweringExt for ResolverAstLowering { None } + fn clone_res(&mut self, source: NodeId, target: NodeId) { + if let Some(res) = self.partial_res_map.get(&source) { + self.partial_res_map.insert(target, *res); + } + } + /// Obtains resolution for a `NodeId` with a single resolution. fn get_partial_res(&self, id: NodeId) -> Option { self.partial_res_map.get(&id).copied() diff --git a/src/test/ui/lint/lint-output-format.rs b/src/test/ui/lint/lint-output-format.rs index 169a98c9483c7..67e8ec8f13b4f 100644 --- a/src/test/ui/lint/lint-output-format.rs +++ b/src/test/ui/lint/lint-output-format.rs @@ -5,6 +5,7 @@ extern crate lint_output_format; //~ ERROR use of unstable library feature use lint_output_format::{foo, bar}; //~ ERROR use of unstable library feature +//~| ERROR use of unstable library feature fn main() { let _x = foo(); diff --git a/src/test/ui/lint/lint-output-format.stderr b/src/test/ui/lint/lint-output-format.stderr index 3bc1d6fc135e5..0db79a1564fa2 100644 --- a/src/test/ui/lint/lint-output-format.stderr +++ b/src/test/ui/lint/lint-output-format.stderr @@ -6,6 +6,14 @@ LL | extern crate lint_output_format; | = help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable +error[E0658]: use of unstable library feature 'unstable_test_feature' + --> $DIR/lint-output-format.rs:7:26 + | +LL | use lint_output_format::{foo, bar}; + | ^^^ + | + = help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable + error[E0658]: use of unstable library feature 'unstable_test_feature' --> $DIR/lint-output-format.rs:7:31 | @@ -15,13 +23,13 @@ LL | use lint_output_format::{foo, bar}; = help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable error[E0658]: use of unstable library feature 'unstable_test_feature' - --> $DIR/lint-output-format.rs:11:14 + --> $DIR/lint-output-format.rs:12:14 | LL | let _y = bar(); | ^^^ | = help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable -error: aborting due to 3 previous errors +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/stability-attribute/stable-in-unstable.rs b/src/test/ui/stability-attribute/stable-in-unstable.rs index 272a1a972340c..226367c399299 100644 --- a/src/test/ui/stability-attribute/stable-in-unstable.rs +++ b/src/test/ui/stability-attribute/stable-in-unstable.rs @@ -44,3 +44,11 @@ mod isolated5 { impl stable_in_unstable_std::old_stable_module::OldTrait for LocalType {} } + +mod isolated6 { + use stable_in_unstable_core::new_unstable_module::{OldTrait}; //~ ERROR use of unstable library feature 'unstable_test_feature' +} + +mod isolated7 { + use stable_in_unstable_core::new_unstable_module::*; //~ ERROR use of unstable library feature 'unstable_test_feature' +} diff --git a/src/test/ui/stability-attribute/stable-in-unstable.stderr b/src/test/ui/stability-attribute/stable-in-unstable.stderr index e123d83584c81..b5e3e5f1202c1 100644 --- a/src/test/ui/stability-attribute/stable-in-unstable.stderr +++ b/src/test/ui/stability-attribute/stable-in-unstable.stderr @@ -34,6 +34,24 @@ LL | impl stable_in_unstable_core::new_unstable_module::OldTrait for LocalTy = note: see issue #1 for more information = help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable -error: aborting due to 4 previous errors +error[E0658]: use of unstable library feature 'unstable_test_feature' + --> $DIR/stable-in-unstable.rs:49:56 + | +LL | use stable_in_unstable_core::new_unstable_module::{OldTrait}; + | ^^^^^^^^ + | + = note: see issue #1 for more information + = help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable + +error[E0658]: use of unstable library feature 'unstable_test_feature' + --> $DIR/stable-in-unstable.rs:53:9 + | +LL | use stable_in_unstable_core::new_unstable_module::*; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #1 for more information + = help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable + +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0658`.