Skip to content

Commit 46244f3

Browse files
committed
Auto merge of rust-lang#99292 - Aaron1011:stability-use-tree, r=cjgillot
Correctly handle path stability for 'use tree' items PR rust-lang#95956 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.
2 parents 8154955 + c7b31d0 commit 46244f3

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

compiler/rustc_ast_lowering/src/item.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
545545
let ident = *ident;
546546
let mut path = path.clone();
547547
for seg in &mut path.segments {
548-
seg.id = self.next_node_id();
548+
// Give the cloned segment the same resolution information
549+
// as the old one (this is needed for stability checking).
550+
let new_id = self.next_node_id();
551+
self.resolver.clone_res(seg.id, new_id);
552+
seg.id = new_id;
549553
}
550554
let span = path.span;
551555

@@ -614,7 +618,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
614618

615619
// Give the segments new node-ids since they are being cloned.
616620
for seg in &mut prefix.segments {
617-
seg.id = self.next_node_id();
621+
// Give the cloned segment the same resolution information
622+
// as the old one (this is needed for stability checking).
623+
let new_id = self.next_node_id();
624+
self.resolver.clone_res(seg.id, new_id);
625+
seg.id = new_id;
618626
}
619627

620628
// Each `use` import is an item and thus are owners of the

compiler/rustc_ast_lowering/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ trait ResolverAstLoweringExt {
160160
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>;
161161
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes>;
162162
fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>>;
163+
// Clones the resolution (if any) on 'source' and applies it
164+
// to 'target'. Used when desugaring a `UseTreeKind::Nested` to
165+
// multiple `UseTreeKind::Simple`s
166+
fn clone_res(&mut self, source: NodeId, target: NodeId);
163167
fn get_label_res(&self, id: NodeId) -> Option<NodeId>;
164168
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
165169
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
@@ -192,6 +196,12 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
192196
None
193197
}
194198

199+
fn clone_res(&mut self, source: NodeId, target: NodeId) {
200+
if let Some(res) = self.partial_res_map.get(&source) {
201+
self.partial_res_map.insert(target, *res);
202+
}
203+
}
204+
195205
/// Obtains resolution for a `NodeId` with a single resolution.
196206
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
197207
self.partial_res_map.get(&id).copied()

src/test/ui/lint/lint-output-format.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
extern crate lint_output_format; //~ ERROR use of unstable library feature
77
use lint_output_format::{foo, bar}; //~ ERROR use of unstable library feature
8+
//~| ERROR use of unstable library feature
89

910
fn main() {
1011
let _x = foo();

src/test/ui/lint/lint-output-format.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | extern crate lint_output_format;
66
|
77
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
88

9+
error[E0658]: use of unstable library feature 'unstable_test_feature'
10+
--> $DIR/lint-output-format.rs:7:26
11+
|
12+
LL | use lint_output_format::{foo, bar};
13+
| ^^^
14+
|
15+
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
16+
917
error[E0658]: use of unstable library feature 'unstable_test_feature'
1018
--> $DIR/lint-output-format.rs:7:31
1119
|
@@ -15,13 +23,13 @@ LL | use lint_output_format::{foo, bar};
1523
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
1624

1725
error[E0658]: use of unstable library feature 'unstable_test_feature'
18-
--> $DIR/lint-output-format.rs:11:14
26+
--> $DIR/lint-output-format.rs:12:14
1927
|
2028
LL | let _y = bar();
2129
| ^^^
2230
|
2331
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
2432

25-
error: aborting due to 3 previous errors
33+
error: aborting due to 4 previous errors
2634

2735
For more information about this error, try `rustc --explain E0658`.

src/test/ui/stability-attribute/stable-in-unstable.rs

+8
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ mod isolated5 {
4444

4545
impl stable_in_unstable_std::old_stable_module::OldTrait for LocalType {}
4646
}
47+
48+
mod isolated6 {
49+
use stable_in_unstable_core::new_unstable_module::{OldTrait}; //~ ERROR use of unstable library feature 'unstable_test_feature'
50+
}
51+
52+
mod isolated7 {
53+
use stable_in_unstable_core::new_unstable_module::*; //~ ERROR use of unstable library feature 'unstable_test_feature'
54+
}

src/test/ui/stability-attribute/stable-in-unstable.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ LL | impl stable_in_unstable_core::new_unstable_module::OldTrait for LocalTy
3434
= note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
3535
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
3636

37-
error: aborting due to 4 previous errors
37+
error[E0658]: use of unstable library feature 'unstable_test_feature'
38+
--> $DIR/stable-in-unstable.rs:49:56
39+
|
40+
LL | use stable_in_unstable_core::new_unstable_module::{OldTrait};
41+
| ^^^^^^^^
42+
|
43+
= note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
44+
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
45+
46+
error[E0658]: use of unstable library feature 'unstable_test_feature'
47+
--> $DIR/stable-in-unstable.rs:53:9
48+
|
49+
LL | use stable_in_unstable_core::new_unstable_module::*;
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51+
|
52+
= note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
53+
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
54+
55+
error: aborting due to 6 previous errors
3856

3957
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)