Skip to content

Commit 4f27d22

Browse files
varkorAvi-D-coder
andcommitted
Initial stability checking on default type parameters
Co-Authored-By: Avi Dessauer <[email protected]>
1 parent 20cfb59 commit 4f27d22

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/librustc_passes/stability.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,13 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
138138
} else {
139139
debug!("annotate: not found, parent = {:?}", self.parent_stab);
140140
if let Some(stab) = self.parent_stab {
141-
// Instability (but not stability) is inherited from the parent.
141+
// Instability (but not typically stability) is inherited from the parent.
142142
// If something is unstable, everything inside it should also be
143143
// considered unstable.
144-
if stab.level.is_unstable() {
144+
// `AnnotationKind::Optional` items also inherit stability, as they may be
145+
// considered stable-by-default items, requiring an instability attribute to
146+
// override this.
147+
if stab.level.is_unstable() || kind == AnnotationKind::Optional {
145148
self.index.stab_map.insert(hir_id, stab);
146149
}
147150
}
@@ -173,7 +176,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
173176
// Propagate unstability. This can happen even for non-staged-api crates in case
174177
// -Zforce-unstable-if-unmarked is set.
175178
if let Some(stab) = self.parent_stab {
176-
if stab.level.is_unstable() {
179+
if stab.level.is_unstable() || kind == AnnotationKind::Optional {
177180
self.index.stab_map.insert(hir_id, stab);
178181
}
179182
}

src/librustc_typeck/astconv.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
697697
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
698698
self.ast_region_to_region(&lt, Some(param)).into()
699699
}
700-
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
700+
(GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => {
701+
if *has_default {
702+
tcx.check_stability(param.def_id, Some(arg.id()), arg.span());
703+
}
704+
701705
self.ast_ty_to_ty(&ty).into()
702706
}
703707
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![crate_type = "lib"]
2+
#![feature(staged_api)]
3+
4+
#![stable(feature = "stable_test_feature", since = "1.0.0")]
5+
6+
#[stable(feature = "stable_test_feature", since = "1.0.0")]
7+
pub trait Trait<#[unstable(feature = "ty", issue = "none")] T = ()> {
8+
#[stable(feature = "stable_test_feature", since = "1.0.0")]
9+
fn foo() -> T;
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// aux-build:unstable_generic_param.rs
2+
3+
extern crate unstable_generic_param;
4+
5+
use unstable_generic_param::Trait;
6+
7+
struct S;
8+
9+
impl Trait<usize> for S {
10+
fn foo() -> usize { 0 }
11+
}
12+
13+
fn main() {
14+
let _ = S;
15+
}

0 commit comments

Comments
 (0)