2022-01-07 11:38:16 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::Res;
|
|
|
|
use rustc_hir::{GenericArg, PathSegment, QPath, TyKind};
|
|
|
|
use rustc_middle::ty;
|
2024-04-29 03:42:13 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2022-01-07 11:38:16 +00:00
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
|
2022-09-05 16:38:55 +00:00
|
|
|
use crate::lints::PassByValueDiag;
|
2022-01-07 11:38:16 +00:00
|
|
|
use crate::{LateContext, LateLintPass, LintContext};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2022-01-07 11:38:16 +00:00
|
|
|
declare_tool_lint! {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to
|
|
|
|
/// always be passed by value. This is usually used for types that are thin wrappers around
|
|
|
|
/// references, so there is no benefit to an extra layer of indirection. (Example: `Ty` which
|
2022-11-28 14:23:49 +00:00
|
|
|
/// is a reference to an `Interned<TyKind>`)
|
2022-01-07 11:38:16 +00:00
|
|
|
pub rustc::PASS_BY_VALUE,
|
|
|
|
Warn,
|
|
|
|
"pass by reference of a type flagged as `#[rustc_pass_by_value]`",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(PassByValue => [PASS_BY_VALUE]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for PassByValue {
|
|
|
|
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) {
|
|
|
|
match &ty.kind {
|
2022-12-28 17:06:11 +00:00
|
|
|
TyKind::Ref(_, hir::MutTy { ty: inner_ty, mutbl: hir::Mutability::Not }) => {
|
2022-01-07 11:38:16 +00:00
|
|
|
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) {
|
|
|
|
if cx.tcx.impl_trait_ref(impl_did).is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 19:07:32 +00:00
|
|
|
if let Some(t) = path_for_pass_by_value(cx, inner_ty) {
|
2024-01-16 03:40:39 +00:00
|
|
|
cx.emit_span_lint(
|
2022-09-16 07:01:02 +00:00
|
|
|
PASS_BY_VALUE,
|
|
|
|
ty.span,
|
2023-01-15 14:02:02 +00:00
|
|
|
PassByValueDiag { ty: t, suggestion: ty.span },
|
2022-09-05 16:38:55 +00:00
|
|
|
);
|
2022-01-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<String> {
|
|
|
|
if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
|
|
|
|
match path.res {
|
2022-01-11 09:28:13 +00:00
|
|
|
Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => {
|
2022-01-10 18:12:28 +00:00
|
|
|
let name = cx.tcx.item_name(def_id).to_ident_string();
|
2022-01-11 19:59:06 +00:00
|
|
|
let path_segment = path.segments.last().unwrap();
|
|
|
|
return Some(format!("{}{}", name, gen_args(cx, path_segment)));
|
2022-01-07 11:38:16 +00:00
|
|
|
}
|
2022-09-16 01:45:33 +00:00
|
|
|
Res::SelfTyAlias { alias_to: did, is_trait_impl: false, .. } => {
|
2023-07-11 21:35:29 +00:00
|
|
|
if let ty::Adt(adt, args) = cx.tcx.type_of(did).instantiate_identity().kind() {
|
2022-03-04 20:28:41 +00:00
|
|
|
if cx.tcx.has_attr(adt.did(), sym::rustc_pass_by_value) {
|
2023-07-11 21:35:29 +00:00
|
|
|
return Some(cx.tcx.def_path_str_with_args(adt.did(), args));
|
2022-01-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-01-11 19:59:06 +00:00
|
|
|
fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
|
2022-01-07 11:38:16 +00:00
|
|
|
if let Some(args) = &segment.args {
|
2022-01-11 19:59:06 +00:00
|
|
|
let params = args
|
2022-01-07 11:38:16 +00:00
|
|
|
.args
|
|
|
|
.iter()
|
2022-01-11 21:28:04 +00:00
|
|
|
.map(|arg| match arg {
|
2022-11-06 14:34:38 +00:00
|
|
|
GenericArg::Lifetime(lt) => lt.to_string(),
|
2022-01-11 19:59:06 +00:00
|
|
|
GenericArg::Type(ty) => {
|
2022-01-27 06:58:33 +00:00
|
|
|
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_else(|_| "_".into())
|
2022-01-07 11:38:16 +00:00
|
|
|
}
|
2024-04-26 13:05:00 +00:00
|
|
|
GenericArg::Const(c) => cx
|
|
|
|
.tcx
|
|
|
|
.sess
|
|
|
|
.source_map()
|
2024-06-03 06:23:28 +00:00
|
|
|
.span_to_snippet(c.span())
|
2024-04-26 13:05:00 +00:00
|
|
|
.unwrap_or_else(|_| "_".into()),
|
2022-01-11 21:28:04 +00:00
|
|
|
GenericArg::Infer(_) => String::from("_"),
|
2022-01-07 11:38:16 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2022-01-11 19:59:06 +00:00
|
|
|
if !params.is_empty() {
|
|
|
|
return format!("<{}>", params.join(", "));
|
2022-01-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::new()
|
|
|
|
}
|