Skip to content
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

feat: rustc_pass_by_value lint attribute #92646

Merged
merged 6 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions compiler/rustc_lint/src/pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand All @@ -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 {
mdibaiee marked this conversation as resolved.
Show resolved Hide resolved
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default();
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_else(|_| String::from("_"));

🤔 don't want empty strings in case the span is broken. Don't know how to even get broken strings here, so i don't think this matters too much :p

Some(snippet)
}
_ => None,
})
.collect::<Vec<_>>();

if !lifetimes.is_empty() {
return format!("<{}>", lifetimes.join(", "));
if !params.is_empty() {
return format!("<{}>", params.join(", "));
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
14 changes: 13 additions & 1 deletion src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

14 changes: 14 additions & 0 deletions src/test/ui-fulldeps/internal-lints/rustc_pass_by_value_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Original file line number Diff line number Diff line change
Expand Up @@ -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