-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deprecate std::intrinsics::transmute
etc, use std::mem::*
instead
#135003
Changes from all commits
561a097
cf0ab86
f1c95c9
7ae494a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,9 +232,18 @@ fn late_report_deprecation( | |
return; | ||
} | ||
|
||
let is_in_effect = depr.is_in_effect(); | ||
let lint = deprecation_lint(is_in_effect); | ||
|
||
// Calculating message for lint involves calling `self.def_path_str`, | ||
// which will by default invoke the expensive `visible_parent_map` query. | ||
// Skip all that work if the lint is allowed anyway. | ||
if tcx.lint_level_at_node(lint, hir_id).0 == Level::Allow { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This first commit is unrelated, it just moves some logic to what I think is a more sensible location. I thought I'd use that logic for this PR but in the end I did not. I can make this a separate PR if you prefer. |
||
|
||
let def_path = with_no_trimmed_paths!(tcx.def_path_str(def_id)); | ||
let def_kind = tcx.def_descr(def_id); | ||
let is_in_effect = depr.is_in_effect(); | ||
|
||
let method_span = method_span.unwrap_or(span); | ||
let suggestion = | ||
|
@@ -250,7 +259,7 @@ fn late_report_deprecation( | |
note: depr.note, | ||
since_kind: deprecated_since_kind(is_in_effect, depr.since), | ||
}; | ||
tcx.emit_node_span_lint(deprecation_lint(is_in_effect), hir_id, method_span, diag); | ||
tcx.emit_node_span_lint(lint, hir_id, method_span, diag); | ||
} | ||
|
||
/// Result of `TyCtxt::eval_stability`. | ||
|
@@ -360,13 +369,7 @@ impl<'tcx> TyCtxt<'tcx> { | |
// hierarchy. | ||
let depr_attr = &depr_entry.attr; | ||
if !skip || depr_attr.is_since_rustc_version() { | ||
// Calculating message for lint involves calling `self.def_path_str`. | ||
// Which by default to calculate visible path will invoke expensive `visible_parent_map` query. | ||
// So we skip message calculation altogether, if lint is allowed. | ||
let lint = deprecation_lint(depr_attr.is_in_effect()); | ||
if self.lint_level_at_node(lint, id).0 != Level::Allow { | ||
late_report_deprecation(self, depr_attr, span, method_span, id, def_id); | ||
} | ||
late_report_deprecation(self, depr_attr, span, method_span, id, def_id); | ||
} | ||
}; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ use std::mem::replace; | |
use std::num::NonZero; | ||
|
||
use rustc_attr_parsing::{ | ||
self as attr, ConstStability, DeprecatedSince, Stability, StabilityLevel, StableSince, | ||
UnstableReason, VERSION_PLACEHOLDER, | ||
self as attr, AllowedThroughUnstableModules, ConstStability, DeprecatedSince, Stability, | ||
StabilityLevel, StableSince, UnstableReason, VERSION_PLACEHOLDER, | ||
}; | ||
use rustc_data_structures::fx::FxIndexMap; | ||
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet}; | ||
|
@@ -20,11 +20,16 @@ use rustc_hir::{FieldDef, Item, ItemKind, TraitRef, Ty, TyKind, Variant}; | |
use rustc_middle::hir::nested_filter; | ||
use rustc_middle::middle::lib_features::{FeatureStability, LibFeatures}; | ||
use rustc_middle::middle::privacy::EffectiveVisibilities; | ||
use rustc_middle::middle::stability::{AllowUnstable, DeprecationEntry, Index}; | ||
use rustc_middle::middle::stability::{ | ||
AllowUnstable, Deprecated, DeprecationEntry, EvalResult, Index, | ||
}; | ||
use rustc_middle::query::Providers; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_middle::ty::print::with_no_trimmed_paths; | ||
use rustc_session::lint; | ||
use rustc_session::lint::builtin::{INEFFECTIVE_UNSTABLE_TRAIT_IMPL, USELESS_DEPRECATED}; | ||
use rustc_session::lint::builtin::{ | ||
DEPRECATED, INEFFECTIVE_UNSTABLE_TRAIT_IMPL, USELESS_DEPRECATED, | ||
}; | ||
use rustc_span::{Span, Symbol, sym}; | ||
use tracing::{debug, info}; | ||
|
||
|
@@ -844,42 +849,95 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { | |
}, | ||
); | ||
|
||
let is_allowed_through_unstable_modules = |def_id| { | ||
self.tcx.lookup_stability(def_id).is_some_and(|stab| match stab.level { | ||
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => { | ||
allowed_through_unstable_modules | ||
if item_is_allowed { | ||
// The item itself is allowed; check whether the path there is also allowed. | ||
let is_allowed_through_unstable_modules: Option<AllowedThroughUnstableModules> = | ||
self.tcx.lookup_stability(def_id).and_then(|stab| match stab.level { | ||
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => { | ||
allowed_through_unstable_modules | ||
} | ||
_ => None, | ||
}); | ||
|
||
if is_allowed_through_unstable_modules.is_none() { | ||
// Check parent modules stability as well if the item the path refers to is itself | ||
// stable. We only emit warnings for unstable path segments if the item is stable | ||
// or allowed because stability is often inherited, so the most common case is that | ||
// both the segments and the item are unstable behind the same feature flag. | ||
// | ||
// We check here rather than in `visit_path_segment` to prevent visiting the last | ||
// path segment twice | ||
// | ||
// We include special cases via #[rustc_allowed_through_unstable_modules] for items | ||
// that were accidentally stabilized through unstable paths before this check was | ||
// added, such as `core::intrinsics::transmute` | ||
let parents = path.segments.iter().rev().skip(1); | ||
for path_segment in parents { | ||
if let Some(def_id) = path_segment.res.opt_def_id() { | ||
// use `None` for id to prevent deprecation check | ||
self.tcx.check_stability_allow_unstable( | ||
def_id, | ||
None, | ||
path.span, | ||
None, | ||
if is_unstable_reexport(self.tcx, id) { | ||
AllowUnstable::Yes | ||
} else { | ||
AllowUnstable::No | ||
}, | ||
); | ||
} | ||
} | ||
_ => false, | ||
}) | ||
}; | ||
|
||
if item_is_allowed && !is_allowed_through_unstable_modules(def_id) { | ||
// Check parent modules stability as well if the item the path refers to is itself | ||
// stable. We only emit warnings for unstable path segments if the item is stable | ||
// or allowed because stability is often inherited, so the most common case is that | ||
// both the segments and the item are unstable behind the same feature flag. | ||
// | ||
// We check here rather than in `visit_path_segment` to prevent visiting the last | ||
// path segment twice | ||
// | ||
// We include special cases via #[rustc_allowed_through_unstable_modules] for items | ||
// that were accidentally stabilized through unstable paths before this check was | ||
// added, such as `core::intrinsics::transmute` | ||
let parents = path.segments.iter().rev().skip(1); | ||
for path_segment in parents { | ||
if let Some(def_id) = path_segment.res.opt_def_id() { | ||
// use `None` for id to prevent deprecation check | ||
self.tcx.check_stability_allow_unstable( | ||
def_id, | ||
None, | ||
path.span, | ||
None, | ||
if is_unstable_reexport(self.tcx, id) { | ||
AllowUnstable::Yes | ||
} else { | ||
AllowUnstable::No | ||
}, | ||
); | ||
} else if let Some(AllowedThroughUnstableModules::WithDeprecation(deprecation)) = | ||
is_allowed_through_unstable_modules | ||
{ | ||
// Similar to above, but we cannot use `check_stability_allow_unstable` as that would | ||
// immediately show the stability error. We just want to know the result and disaplay | ||
// our own kind of error. | ||
let parents = path.segments.iter().rev().skip(1); | ||
for path_segment in parents { | ||
if let Some(def_id) = path_segment.res.opt_def_id() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an unfortunate duplication of the loop above, but I didn't find a good way to deduplicate that code. |
||
// use `None` for id to prevent deprecation check | ||
let eval_result = self.tcx.eval_stability_allow_unstable( | ||
def_id, | ||
None, | ||
path.span, | ||
None, | ||
if is_unstable_reexport(self.tcx, id) { | ||
AllowUnstable::Yes | ||
} else { | ||
AllowUnstable::No | ||
}, | ||
); | ||
let is_allowed = matches!(eval_result, EvalResult::Allow); | ||
if !is_allowed { | ||
// Calculating message for lint involves calling `self.def_path_str`, | ||
// which will by default invoke the expensive `visible_parent_map` query. | ||
// Skip all that work if the lint is allowed anyway. | ||
if self.tcx.lint_level_at_node(DEPRECATED, id).0 | ||
== lint::Level::Allow | ||
{ | ||
return; | ||
} | ||
// Show a deprecation message. | ||
let def_path = | ||
with_no_trimmed_paths!(self.tcx.def_path_str(def_id)); | ||
let def_kind = self.tcx.def_descr(def_id); | ||
let diag = Deprecated { | ||
sub: None, | ||
kind: def_kind.to_owned(), | ||
path: def_path, | ||
note: Some(deprecation), | ||
since_kind: lint::DeprecatedSinceKind::InEffect, | ||
}; | ||
self.tcx.emit_node_span_lint( | ||
DEPRECATED, | ||
id, | ||
method_span.unwrap_or(path.span), | ||
diag, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1897,7 +1897,11 @@ pub const fn forget<T: ?Sized>(_: T) { | |||||||||||||||||
/// } | ||||||||||||||||||
/// ``` | ||||||||||||||||||
#[stable(feature = "rust1", since = "1.0.0")] | ||||||||||||||||||
#[rustc_allowed_through_unstable_modules] | ||||||||||||||||||
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)] | ||||||||||||||||||
#[cfg_attr( | ||||||||||||||||||
not(bootstrap), | ||||||||||||||||||
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead" | ||||||||||||||||||
)] | ||||||||||||||||||
#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")] | ||||||||||||||||||
#[rustc_diagnostic_item = "transmute"] | ||||||||||||||||||
#[rustc_nounwind] | ||||||||||||||||||
|
@@ -4325,7 +4329,11 @@ pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + ?Sized, M>(_ptr: *cons | |||||||||||||||||
/// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append | ||||||||||||||||||
#[doc(alias = "memcpy")] | ||||||||||||||||||
#[stable(feature = "rust1", since = "1.0.0")] | ||||||||||||||||||
#[rustc_allowed_through_unstable_modules] | ||||||||||||||||||
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)] | ||||||||||||||||||
#[cfg_attr( | ||||||||||||||||||
not(bootstrap), | ||||||||||||||||||
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead" | ||||||||||||||||||
)] | ||||||||||||||||||
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")] | ||||||||||||||||||
#[inline(always)] | ||||||||||||||||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces | ||||||||||||||||||
|
@@ -4429,7 +4437,11 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us | |||||||||||||||||
/// ``` | ||||||||||||||||||
#[doc(alias = "memmove")] | ||||||||||||||||||
#[stable(feature = "rust1", since = "1.0.0")] | ||||||||||||||||||
#[rustc_allowed_through_unstable_modules] | ||||||||||||||||||
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)] | ||||||||||||||||||
#[cfg_attr( | ||||||||||||||||||
not(bootstrap), | ||||||||||||||||||
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead" | ||||||||||||||||||
)] | ||||||||||||||||||
Comment on lines
+4441
to
+4444
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea how to implement that with reasonable effort, sorry. This is a rustc-internal attribute so it doesn't have to be super pretty. |
||||||||||||||||||
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")] | ||||||||||||||||||
#[inline(always)] | ||||||||||||||||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces | ||||||||||||||||||
|
@@ -4512,7 +4524,11 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) { | |||||||||||||||||
/// ``` | ||||||||||||||||||
#[doc(alias = "memset")] | ||||||||||||||||||
#[stable(feature = "rust1", since = "1.0.0")] | ||||||||||||||||||
#[rustc_allowed_through_unstable_modules] | ||||||||||||||||||
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)] | ||||||||||||||||||
#[cfg_attr( | ||||||||||||||||||
not(bootstrap), | ||||||||||||||||||
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead" | ||||||||||||||||||
)] | ||||||||||||||||||
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")] | ||||||||||||||||||
#[inline(always)] | ||||||||||||||||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ use std::{fmt, iter}; | |
|
||
use arrayvec::ArrayVec; | ||
use rustc_abi::{ExternAbi, VariantIdx}; | ||
use rustc_attr_parsing::{ConstStability, Deprecation, Stability, StableSince}; | ||
use rustc_attr_parsing::{ | ||
AllowedThroughUnstableModules, ConstStability, Deprecation, Stability, StableSince, | ||
}; | ||
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; | ||
use rustc_hir::def::{CtorKind, DefKind, Res}; | ||
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId}; | ||
|
@@ -406,15 +408,19 @@ impl Item { | |
// were never supposed to work at all. | ||
let stab = self.stability(tcx)?; | ||
if let rustc_attr_parsing::StabilityLevel::Stable { | ||
allowed_through_unstable_modules: true, | ||
allowed_through_unstable_modules: Some(note), | ||
.. | ||
} = stab.level | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @notriddle I am not sure what you mean by "translate" in your comment below. rustc itself also does not currently consider all of these paths deprecated (only the ones where a deprecation message has been set), but it seems like a good idea to have rustdoc de-emphasize all of them as a form of soft deprecation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not all of these will have a message though, so things will still look odd for those that don't. It seems nice to have rustdoc de-emphasize these items even before we have a proper deprecation message for them, but I am not sure how to do that. I can easily change the code here to only show "deprecated" when there is a message, but then the "wrong" path would show up in search results again. Would be nice to have a sort of soft deprecation where rustc doesn't warn but also rustdoc doesn't point people there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give an example where (1) no deprecation message is being added (2) the "bad" path is shorter than, or the same length as, the stable one? If there aren't any, then nothing needs to be done and we can just remove the deprecation tag whenever the message is None, because intra-doc links and search results both already prefer shorter paths. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That doesn't seem to be true, given that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's counting components, not characters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. I haven't yet checked the other uses of allowed_through_unstable_modules; I plan to do that once this PR lands. |
||
let note = match note { | ||
AllowedThroughUnstableModules::WithDeprecation(note) => Some(note), | ||
// FIXME: Would be better to say *something* here about the *path* being | ||
// deprecated rather than the item. | ||
AllowedThroughUnstableModules::WithoutDeprecation => None, | ||
}; | ||
Some(Deprecation { | ||
// FIXME(#131676, #135003): when a note is added to this stability tag, | ||
// translate it here | ||
since: rustc_attr_parsing::DeprecatedSince::Unspecified, | ||
note: None, | ||
note, | ||
suggestion: None, | ||
}) | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(to implement the way I suggested before)
you should use
meta_item_list
, find one that has namedeprecated
, then usevalue_str
on that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then we'd silently accept arbitrary other meta items, right? Currently the
rustc_attr!
can describe thetemplate!
of the attribute which AFAIK gets us some checks that reject any other shapes; but for custom inner meta item lists I don't think there is a correspondingtemplate!
?I'm not convinced that is worth the effort, but if the reviewer insists I can do it.