Skip to content

Commit 109d416

Browse files
varkorAvi-D-coder
andcommitted
Permit and require stability attributes on default type parameters
Co-Authored-By: Avi Dessauer <[email protected]>
1 parent 04243e3 commit 109d416

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/librustc_passes/reachable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
309309
| Node::Ctor(..)
310310
| Node::Field(_)
311311
| Node::Ty(_)
312+
| Node::GenericParam(_)
312313
| Node::MacroDef(_) => {}
313314
_ => {
314315
bug!(

src/librustc_passes/stability.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,20 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
278278
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
279279
self.annotate(md.hir_id, &md.attrs, md.span, AnnotationKind::Required, |_| {});
280280
}
281+
282+
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
283+
let kind = match &p.kind {
284+
// FIXME(const_generics:defaults)
285+
hir::GenericParamKind::Type { default, .. } if default.is_some() => {
286+
AnnotationKind::Required
287+
}
288+
_ => AnnotationKind::Prohibited,
289+
};
290+
291+
self.annotate(p.hir_id, &p.attrs, p.span, kind, |v| {
292+
intravisit::walk_generic_param(v, p);
293+
});
294+
}
281295
}
282296

283297
struct MissingStabilityAnnotations<'a, 'tcx> {
@@ -348,6 +362,17 @@ impl<'a, 'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'a, 'tcx> {
348362
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
349363
self.check_missing_stability(md.hir_id, md.span, "macro");
350364
}
365+
366+
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
367+
match &p.kind {
368+
// FIXME(const_generics:defaults)
369+
hir::GenericParamKind::Type { default, .. } if default.is_some() => {
370+
self.check_missing_stability(p.hir_id, p.span, "default type parameter");
371+
}
372+
_ => {}
373+
}
374+
intravisit::walk_generic_param(self, p);
375+
}
351376
}
352377

353378
fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {

src/librustc_privacy/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,20 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
946946
module_id = self.tcx.hir().get_parent_node(module_id);
947947
}
948948
}
949+
950+
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
951+
// Generic parameters cannot be directly accessed by users, so restricting privacy is
952+
// unimportant here.
953+
// However, parameters must be (at least) `Reachable` in order for stability requirements to
954+
// be enforced (see `check_missing_stability` in `src/librustc_passes/stability.rs`).
955+
match &p.kind {
956+
// FIXME(const_generics:defaults)
957+
hir::GenericParamKind::Type { default, .. } if default.is_some() => {
958+
self.update(p.hir_id, Some(AccessLevel::Reachable));
959+
}
960+
_ => {}
961+
}
962+
}
949963
}
950964

951965
impl ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {

0 commit comments

Comments
 (0)