Skip to content

Commit 7124472

Browse files
committed
Add check for an unstable attribute on the re-export, and update the test
1 parent d0b1b4b commit 7124472

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,11 @@ fn suggestion_for_allocator_api(
307307
}
308308

309309
/// An override option for eval_stability.
310-
pub enum EvalOverride {
310+
pub enum AllowUnstable {
311311
/// Don't emit an unstable error for the item
312-
AllowUnstable,
312+
Yes,
313313
/// Handle the item normally
314-
None,
315-
}
316-
317-
impl Default for EvalOverride {
318-
fn default() -> Self {
319-
Self::None
320-
}
314+
No,
321315
}
322316

323317
impl<'tcx> TyCtxt<'tcx> {
@@ -337,7 +331,7 @@ impl<'tcx> TyCtxt<'tcx> {
337331
span: Span,
338332
method_span: Option<Span>,
339333
) -> EvalResult {
340-
self.eval_stability_override(def_id, id, span, method_span, Default::default())
334+
self.eval_stability_override(def_id, id, span, method_span, AllowUnstable::No)
341335
}
342336

343337
/// Evaluates the stability of an item.
@@ -357,7 +351,7 @@ impl<'tcx> TyCtxt<'tcx> {
357351
id: Option<HirId>,
358352
span: Span,
359353
method_span: Option<Span>,
360-
eval_override: EvalOverride,
354+
eval_override: AllowUnstable,
361355
) -> EvalResult {
362356
// Deprecated attributes apply in-crate and cross-crate.
363357
if let Some(id) = id {
@@ -455,7 +449,7 @@ impl<'tcx> TyCtxt<'tcx> {
455449
}
456450
}
457451

458-
if matches!(eval_override, EvalOverride::AllowUnstable) {
452+
if matches!(eval_override, AllowUnstable::Yes) {
459453
return EvalResult::Allow;
460454
}
461455

@@ -485,7 +479,7 @@ impl<'tcx> TyCtxt<'tcx> {
485479
span: Span,
486480
method_span: Option<Span>,
487481
) {
488-
self.check_stability_override(def_id, id, span, method_span, Default::default())
482+
self.check_stability_override(def_id, id, span, method_span, AllowUnstable::No)
489483
}
490484

491485
/// Checks if an item is stable or error out.
@@ -503,7 +497,7 @@ impl<'tcx> TyCtxt<'tcx> {
503497
id: Option<HirId>,
504498
span: Span,
505499
method_span: Option<Span>,
506-
eval_override: EvalOverride,
500+
eval_override: AllowUnstable,
507501
) {
508502
self.check_optional_stability(
509503
def_id,
@@ -529,7 +523,7 @@ impl<'tcx> TyCtxt<'tcx> {
529523
id: Option<HirId>,
530524
span: Span,
531525
method_span: Option<Span>,
532-
eval_override: EvalOverride,
526+
eval_override: AllowUnstable,
533527
unmarked: impl FnOnce(Span, DefId),
534528
) {
535529
let soft_handler = |lint, span, msg: &_| {

compiler/rustc_passes/src/stability.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::intravisit::{self, Visitor};
1313
use rustc_hir::{FieldDef, Generics, HirId, Item, TraitRef, Ty, TyKind, Variant};
1414
use rustc_middle::hir::nested_filter;
1515
use rustc_middle::middle::privacy::AccessLevels;
16-
use rustc_middle::middle::stability::{DeprecationEntry, EvalOverride, Index};
16+
use rustc_middle::middle::stability::{AllowUnstable, DeprecationEntry, Index};
1717
use rustc_middle::ty::{self, query::Providers, TyCtxt};
1818
use rustc_session::lint;
1919
use rustc_session::lint::builtin::{INEFFECTIVE_UNSTABLE_TRAIT_IMPL, USELESS_DEPRECATED};
@@ -813,28 +813,29 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
813813
Some(id),
814814
path.span,
815815
method_span,
816-
if is_path_reexport(self.tcx, id) {
817-
EvalOverride::AllowUnstable
818-
} else {
819-
EvalOverride::None
820-
},
816+
if is_unstable_reexport(self.tcx, id) { AllowUnstable::Yes } else { AllowUnstable::No },
821817
)
822818
}
823819
intravisit::walk_path(self, path)
824820
}
825821
}
826822

827-
/// Check whether a path is a `use` item
828-
fn is_path_reexport<'tcx>(tcx: TyCtxt<'tcx>, id: hir::HirId) -> bool {
823+
/// Check whether a path is a `use` item that has been marked as unstable.
824+
fn is_unstable_reexport<'tcx>(tcx: TyCtxt<'tcx>, id: hir::HirId) -> bool {
829825
// Get the LocalDefId so we can lookup the item to check the kind.
830826
let Some(def_id) = tcx.hir().opt_local_def_id(id) else { return false};
831827

832-
// If this is a path that isn't a use, we don't need to do anything special
833-
if !matches!(tcx.hir().item(hir::ItemId { def_id }).kind, ItemKind::Use(..)) {
828+
let Some(stab) = tcx.stability().local_stability(def_id) else {
829+
return false;
830+
};
831+
832+
if stab.level.is_stable() {
833+
// The re-export is not marked as unstable, don't override
834834
return false;
835835
}
836836

837-
if tcx.visibility(def_id) != ty::Visibility::Public {
837+
// If this is a path that isn't a use, we don't need to do anything special
838+
if !matches!(tcx.hir().item(hir::ItemId { def_id }).kind, ItemKind::Use(..)) {
838839
return false;
839840
}
840841

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
2424
use rustc_hir::intravisit::{walk_generics, Visitor as _};
2525
use rustc_hir::lang_items::LangItem;
2626
use rustc_hir::{GenericArg, GenericArgs};
27+
use rustc_middle::middle::stability::AllowUnstable;
2728
use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, Subst, SubstsRef};
2829
use rustc_middle::ty::GenericParamDefKind;
2930
use rustc_middle::ty::{self, Const, DefIdTree, EarlyBinder, Ty, TyCtxt, TypeFoldable};
@@ -426,7 +427,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
426427
Some(arg.id()),
427428
arg.span(),
428429
None,
429-
Default::default(),
430+
AllowUnstable::No,
430431
|_, _| {
431432
// Default generic parameters may not be marked
432433
// with stability attributes, i.e. when the

src/test/ui/stability-attribute/allow-unstable-reexport.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
extern crate lint_stability;
99
extern crate lint_stability_reexport;
1010

11-
#[stable(feature = "lint_stability", since = "1.0.0")]
11+
#[unstable(feature = "unstable_test_feature", issue = "none")]
1212
pub use lint_stability::unstable;
13+
1314
// We want to confirm that using a re-export through another crate behaves
1415
// the same way as using an item directly
16+
#[unstable(feature = "unstable_test_feature", issue = "none")]
17+
pub use lint_stability_reexport::unstable_text;
18+
19+
// Ensure items which aren't marked as unstable can't re-export unstable items
1520
#[stable(feature = "lint_stability", since = "1.0.0")]
16-
pub use lint_stability_reexport::unstable_text;
21+
pub use lint_stability::unstable as unstable2;
22+
//~^ ERROR use of unstable library feature 'unstable_test_feature'
1723

1824
fn main() {
1925
// Since we didn't enable the feature in this crate, we still can't
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
error[E0658]: use of unstable library feature 'unstable_test_feature'
2-
--> $DIR/allow-unstable-reexport.rs:21:5
2+
--> $DIR/allow-unstable-reexport.rs:20:9
3+
|
4+
LL | pub use lint_stability::unstable as unstable2;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
8+
9+
error[E0658]: use of unstable library feature 'unstable_test_feature'
10+
--> $DIR/allow-unstable-reexport.rs:26:5
311
|
412
LL | unstable();
513
| ^^^^^^^^
614
|
715
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
816

917
error[E0658]: use of unstable library feature 'unstable_test_feature': text
10-
--> $DIR/allow-unstable-reexport.rs:22:5
18+
--> $DIR/allow-unstable-reexport.rs:27:5
1119
|
1220
LL | unstable_text();
1321
| ^^^^^^^^^^^^^
1422
|
1523
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
1624

17-
error: aborting due to 2 previous errors
25+
error: aborting due to 3 previous errors
1826

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

0 commit comments

Comments
 (0)