Skip to content

Commit ecaf1f5

Browse files
committed
Add future deprecation warning to rustdoc
1 parent c08902b commit ecaf1f5

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/librustc/middle/stability.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ pub fn check_unstable_api_usage<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
470470
tcx.hir.krate().visit_all_item_likes(&mut checker.as_deep_visitor());
471471
}
472472

473+
/// Check whether an item marked with `deprecated(since="X")` is currently
474+
/// deprecated (i.e. whether X is not greater than the current rustc version).
475+
pub fn deprecation_in_effect(since: &str) -> bool {
476+
fn parse_version(ver: &str) -> Vec<u32> {
477+
// We ignore non-integer components of the version (e.g. "nightly").
478+
ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect()
479+
}
480+
481+
if let Some(rustc) = option_env!("CFG_RELEASE") {
482+
let since: Vec<u32> = parse_version(since);
483+
let rustc: Vec<u32> = parse_version(rustc);
484+
// We simply treat invalid `since` attributes as relating to a previous
485+
// Rust version, thus always displaying the warning.
486+
if since.len() != 3 {
487+
return true;
488+
}
489+
since <= rustc
490+
} else {
491+
// By default, a deprecation warning applies to
492+
// the current version of the compiler.
493+
true
494+
}
495+
}
496+
473497
struct Checker<'a, 'tcx: 'a> {
474498
tcx: TyCtxt<'a, 'tcx, 'tcx>,
475499
}
@@ -559,33 +583,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
559583
// Deprecated attributes apply in-crate and cross-crate.
560584
if let Some(id) = id {
561585
if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) {
562-
fn deprecation_in_effect(since: Option<&str>, rustc: Option<&str>) -> bool {
563-
fn parse_version(ver: &str) -> Vec<u32> {
564-
// We ignore non-integer components of the version (e.g. "nightly").
565-
ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect()
566-
}
567-
568-
if since.is_none() || rustc.is_none() {
569-
// By default, a deprecation warning applies to
570-
// the current version of the compiler.
571-
true
572-
} else {
573-
let since: Vec<u32> = parse_version(since.unwrap());
574-
let rustc: Vec<u32> = parse_version(rustc.unwrap());
575-
// We simply treat invalid `since` attributes as relating to a previous
576-
// Rust version, thus always displaying the warning.
577-
if since.len() != 3 {
578-
return true;
579-
}
580-
since <= rustc
581-
}
582-
}
583-
584586
// If the deprecation is scheduled for a future Rust
585587
// version, then we should display no warning message.
586588
let deprecated_in_future_version = if let Some(sym) = depr_entry.attr.since {
587589
let since = sym.as_str();
588-
!deprecation_in_effect(Some(since.as_ref()), option_env!("CFG_RELEASE"))
590+
!deprecation_in_effect(since.as_ref())
589591
} else {
590592
false
591593
};

src/librustdoc/html/render.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -2108,9 +2108,15 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
21082108
} else {
21092109
String::new()
21102110
};
2111-
let text = format!("Deprecated{}{}",
2112-
since,
2113-
MarkdownHtml(&deprecated_reason));
2111+
let text = if stability::deprecation_in_effect(&stab.deprecated_since) {
2112+
format!("Deprecated{}{}",
2113+
since,
2114+
MarkdownHtml(&deprecated_reason))
2115+
} else {
2116+
format!("This will be deprecated in {}{}",
2117+
Escape(&stab.deprecated_since),
2118+
MarkdownHtml(&deprecated_reason))
2119+
};
21142120
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
21152121
};
21162122

@@ -2160,7 +2166,15 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
21602166
String::new()
21612167
};
21622168

2163-
let text = format!("Deprecated{}{}", since, MarkdownHtml(&note));
2169+
let text = if stability::deprecation_in_effect(&depr.since) {
2170+
format!("Deprecated{}{}",
2171+
since,
2172+
MarkdownHtml(&note))
2173+
} else {
2174+
format!("This will be deprecated in {}{}",
2175+
Escape(&depr.since),
2176+
MarkdownHtml(&note))
2177+
};
21642178
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
21652179
}
21662180

0 commit comments

Comments
 (0)