rustc_pass_by_value: handle generic and const type parameters

This commit is contained in:
Mahdi Dibaiee 2022-01-11 19:59:06 +00:00
parent a6762e962e
commit 959bf2bc2e
No known key found for this signature in database
GPG Key ID: BABA115BDF0C598A
5 changed files with 74 additions and 16 deletions

View File

@ -51,15 +51,13 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
match path.res {
Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => {
let name = cx.tcx.item_name(def_id).to_ident_string();
return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
let path_segment = path.segments.last().unwrap();
return Some(format!("{}{}", name, gen_args(cx, path_segment)));
}
Res::SelfTy(None, Some((did, _))) => {
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
if cx.tcx.has_attr(adt.did, sym::rustc_pass_by_value) {
let name = cx.tcx.item_name(adt.did).to_ident_string();
let param =
substs.first().map(|s| format!("<{}>", s)).unwrap_or("".to_string());
return Some(format!("{}{}", name, param));
return Some(cx.tcx.def_path_str_with_substs(adt.did, substs));
}
}
}
@ -70,22 +68,29 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
None
}
fn gen_args(segment: &PathSegment<'_>) -> String {
fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
if let Some(args) = &segment.args {
let lifetimes = args
let params = args
.args
.iter()
.filter_map(|arg| {
if let GenericArg::Lifetime(lt) = arg {
Some(lt.name.ident().to_string())
} else {
None
.filter_map(|arg| match arg {
GenericArg::Lifetime(lt) => Some(lt.name.ident().to_string()),
GenericArg::Type(ty) => {
let snippet =
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_default();
Some(snippet)
}
GenericArg::Const(c) => {
let snippet =
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default();
Some(snippet)
}
_ => None,
})
.collect::<Vec<_>>();
if !lifetimes.is_empty() {
return format!("<{}>", lifetimes.join(", "));
if !params.is_empty() {
return format!("<{}>", params.join(", "));
}
}

View File

@ -98,4 +98,19 @@ impl CustomStruct {
}
}
#[rustc_pass_by_value]
struct WithParameters<T, const N: usize, M = u32> {
slice: [T; N],
m: M,
}
impl<T> WithParameters<T, 1> {
fn test(
value: WithParameters<T, 1>,
reference: &WithParameters<T, 1>, //~ ERROR passing `WithParameters<T, 1>` by reference
reference_with_m: &WithParameters<T, 1, u32>, //~ ERROR passing `WithParameters<T, 1, u32>` by reference
) {
}
}
fn main() {}

View File

@ -100,5 +100,17 @@ error: passing `CustomAlias<>` by reference
LL | reference: &CustomAlias,
| ^^^^^^^^^^^^ help: try passing by value: `CustomAlias<>`
error: aborting due to 16 previous errors
error: passing `WithParameters<T, 1>` by reference
--> $DIR/rustc_pass_by_value.rs:110:20
|
LL | reference: &WithParameters<T, 1>,
| ^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters<T, 1>`
error: passing `WithParameters<T, 1, u32>` by reference
--> $DIR/rustc_pass_by_value.rs:111:27
|
LL | reference_with_m: &WithParameters<T, 1, u32>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters<T, 1, u32>`
error: aborting due to 18 previous errors

View File

@ -37,4 +37,18 @@ impl Foo {
fn with_ref(&self) {} //~ ERROR passing `Foo` by reference
}
#[rustc_pass_by_value]
struct WithParameters<T, const N: usize, M = u32> {
slice: [T; N],
m: M,
}
impl<T> WithParameters<T, 1> {
fn with_ref(&self) {} //~ ERROR passing `WithParameters<T, 1_usize>` by reference
}
impl<T> WithParameters<T, 1, u8> {
fn with_ref(&self) {} //~ ERROR passing `WithParameters<T, 1_usize, u8>` by reference
}
fn main() {}

View File

@ -22,5 +22,17 @@ error: passing `Foo` by reference
LL | fn with_ref(&self) {}
| ^^^^^ help: try passing by value: `Foo`
error: aborting due to 3 previous errors
error: passing `WithParameters<T, 1_usize>` by reference
--> $DIR/rustc_pass_by_value_self.rs:47:17
|
LL | fn with_ref(&self) {}
| ^^^^^ help: try passing by value: `WithParameters<T, 1_usize>`
error: passing `WithParameters<T, 1_usize, u8>` by reference
--> $DIR/rustc_pass_by_value_self.rs:51:17
|
LL | fn with_ref(&self) {}
| ^^^^^ help: try passing by value: `WithParameters<T, 1_usize, u8>`
error: aborting due to 5 previous errors