mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-06 20:13:42 +00:00
rustc_pass_by_value: handle generic and const type parameters
This commit is contained in:
parent
a6762e962e
commit
959bf2bc2e
@ -51,15 +51,13 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
|
|||||||
match path.res {
|
match path.res {
|
||||||
Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => {
|
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();
|
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, _))) => {
|
Res::SelfTy(None, Some((did, _))) => {
|
||||||
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
|
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
|
||||||
if cx.tcx.has_attr(adt.did, sym::rustc_pass_by_value) {
|
if cx.tcx.has_attr(adt.did, sym::rustc_pass_by_value) {
|
||||||
let name = cx.tcx.item_name(adt.did).to_ident_string();
|
return Some(cx.tcx.def_path_str_with_substs(adt.did, substs));
|
||||||
let param =
|
|
||||||
substs.first().map(|s| format!("<{}>", s)).unwrap_or("".to_string());
|
|
||||||
return Some(format!("{}{}", name, param));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,22 +68,29 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_args(segment: &PathSegment<'_>) -> String {
|
fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
|
||||||
if let Some(args) = &segment.args {
|
if let Some(args) = &segment.args {
|
||||||
let lifetimes = args
|
let params = args
|
||||||
.args
|
.args
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|arg| {
|
.filter_map(|arg| match arg {
|
||||||
if let GenericArg::Lifetime(lt) = arg {
|
GenericArg::Lifetime(lt) => Some(lt.name.ident().to_string()),
|
||||||
Some(lt.name.ident().to_string())
|
GenericArg::Type(ty) => {
|
||||||
} else {
|
let snippet =
|
||||||
None
|
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<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if !lifetimes.is_empty() {
|
if !params.is_empty() {
|
||||||
return format!("<{}>", lifetimes.join(", "));
|
return format!("<{}>", params.join(", "));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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() {}
|
fn main() {}
|
||||||
|
@ -100,5 +100,17 @@ error: passing `CustomAlias<>` by reference
|
|||||||
LL | reference: &CustomAlias,
|
LL | reference: &CustomAlias,
|
||||||
| ^^^^^^^^^^^^ help: try passing by value: `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
|
||||||
|
|
||||||
|
@ -37,4 +37,18 @@ impl Foo {
|
|||||||
fn with_ref(&self) {} //~ ERROR passing `Foo` by reference
|
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() {}
|
fn main() {}
|
||||||
|
@ -22,5 +22,17 @@ error: passing `Foo` by reference
|
|||||||
LL | fn with_ref(&self) {}
|
LL | fn with_ref(&self) {}
|
||||||
| ^^^^^ help: try passing by value: `Foo`
|
| ^^^^^ 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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user