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

Par iter #1

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ for_each = { path = "lints/for_each", features = ["rlib"] }
filter = { path = "lints/filter", features = ["rlib"] }
fold = { path = "lints/fold", features = ["rlib"] }
par_fold = { path = "lints/par_fold", features = ["rlib"] }
par_iter = { path = "lints/par_iter", features = ["rlib"] }

dylint_linting = { version = "2.5.0" }

Expand All @@ -27,6 +28,7 @@ members = [
"lints/filter",
"lints/fold",
"lints/par_fold",
"lints/par_iter",
"utils",
]

Expand Down
1 change: 1 addition & 0 deletions lints/par_iter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
29 changes: 29 additions & 0 deletions lints/par_iter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "par_iter"
version = "0.1.0"
authors = ["authors go here"]
description = "description goes here"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
clippy_utils = { workspace = true }
utils = { workspace = true }
dylint_linting = "2.5.0"

[dev-dependencies]
dylint_testing = "2.5.0"
rayon = "1.8.0"

[package.metadata.rust-analyzer]
rustc_private = true

[features]
rlib = ["dylint_linting/constituent"]

[[example]]
name = "main"
path = "ui/main.rs"
17 changes: 17 additions & 0 deletions lints/par_iter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# template

### What it does

### Why is this bad?

### Known problems
Remove if none.

### Example
```rust
// example code where a warning is issued
```
Use instead:
```rust
// example code that does not raise a warning
```
182 changes: 182 additions & 0 deletions lints/par_iter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#![feature(rustc_private)]
#![warn(unused_extern_crates)]
#![feature(let_chains)]

extern crate rustc_errors;
extern crate rustc_hir;
extern crate rustc_span;

use clippy_utils::{get_trait_def_id, ty::implements_trait};
use rustc_errors::Applicability;
use rustc_hir::{
def::Res,
intravisit::{walk_expr, Visitor},
Expr, ExprKind, Node,
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_span::sym;
use utils::span_to_snippet_macro;

dylint_linting::declare_late_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Known problems
/// Remove if none.
///
/// ### Example
/// ```rust
/// // example code where a warning is issued
/// ```
/// Use instead:
/// ```rust
/// // example code that does not raise a warning
/// ```
pub PAR_ITER,
Warn,
"suggest using par iter"
}

struct ClosureVisitor<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
is_valid: bool,
}

struct Validator<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
is_valid: bool,
}
impl<'a, 'tcx> Visitor<'_> for ClosureVisitor<'a, 'tcx> {
fn visit_expr(&mut self, ex: &Expr) {
match ex.kind {
ExprKind::Path(path) => {
let res: Res = self.cx.typeck_results().qpath_res(&path, ex.hir_id);

if let Res::Local(hir_id) = res {
let parent = self.cx.tcx.hir().get_parent(hir_id);
match parent {
Node::Local(local) => {
if let Some(expr) = local.init {
let ty =
self.cx.tcx.typeck(expr.hir_id.owner).node_type(expr.hir_id);
let implements_send = self
.cx
.tcx
.get_diagnostic_item(sym::Send)
.map_or(false, |id| implements_trait(self.cx, ty, id, &[]));
let implements_sync = self
.cx
.tcx
.get_diagnostic_item(sym::Sync)
.map_or(false, |id| implements_trait(self.cx, ty, id, &[]));
let is_copy: bool = self
.cx
.tcx
.get_diagnostic_item(sym::Copy)
.map_or(false, |id| implements_trait(self.cx, ty, id, &[]));
let valid = is_copy || (implements_send && implements_sync);
self.is_valid = self.is_valid && valid;
};
}
_ => {}
}
}
}
_ => walk_expr(self, ex),
}
}
}

impl<'a, 'tcx> Visitor<'_> for Validator<'a, 'tcx> {
fn visit_expr(&mut self, ex: &Expr) {
match ex.kind {
ExprKind::Closure(closure) => {
let hir = self.cx.tcx.hir();
let node = hir.get(closure.body.hir_id);
if let Node::Expr(expr) = node {
let mut closure_visitor = ClosureVisitor {
cx: self.cx,
is_valid: true,
};
closure_visitor.visit_expr(expr);
self.is_valid = self.is_valid && closure_visitor.is_valid;
}
}
_ => walk_expr(self, ex),
}
}
}

impl<'tcx> LateLintPass<'tcx> for ParIter {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(path, _recv, _args, _span) = &expr.kind {
let ident_name = &path.ident.name.to_string()[..];
let src_map = cx.sess().source_map();
let mut suggestion = span_to_snippet_macro(src_map, expr.span);
match ident_name {
"into_iter" => suggestion = suggestion.replace("into_iter", "into_par_iter"),

"iter" => suggestion = suggestion.replace("iter", "par_iter"),
"iter_mut" => suggestion = suggestion.replace("iter_mut", "par_iter_mut"),
_ => return,
}

let ty = cx.typeck_results().expr_ty(expr);

let mut implements_par_iter = false;

let trait_defs = vec![
get_trait_def_id(cx, &["rayon", "iter", "IntoParallelIterator"]),
get_trait_def_id(cx, &["rayon", "iter", "ParallelIterator"]),
// @todo get_trait_def_id(cx, &["rayon", "iter", "IndexedParallelIterator"]),
// @todo get_trait_def_id(cx, &["rayon", "iter", "IntoParallelRefIterator"]),
// @todo get_trait_def_id(cx, &["rayon", "iter", "IntoParallelRefMutIterator"]),
];

for t in trait_defs {
if let Some(trait_def_id) = t {
implements_par_iter =
implements_par_iter || implements_trait(cx, ty, trait_def_id, &[]);
}
}

if !implements_par_iter {
return;
}

// check that all types inside the closures are Send and sync or Copy
let mut validator = Validator { cx, is_valid: true };

let parent_node = cx.tcx.hir().get_parent(expr.hir_id);
match parent_node {
Node::Expr(expr) => {
validator.visit_expr(expr);
}
// Handle other kinds of parent nodes as needed
_ => {}
}
if !validator.is_valid {
return;
}

cx.struct_span_lint(
PAR_ITER,
expr.span,
"found iterator that can be parallelized",
|diag| {
diag.multipart_suggestion(
"try using a parallel iterator",
vec![(expr.span, suggestion)],
Applicability::MachineApplicable,
)
},
);
}
}
}

#[test]
fn ui() {
dylint_testing::ui_test_examples(env!("CARGO_PKG_NAME"));
}
100 changes: 100 additions & 0 deletions lints/par_iter/ui/main.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// run-rustfix

#[allow(unused_imports)]
use rayon::prelude::*;
// use std::collections::LinkedList;
use std::rc::Rc;

fn main() {
warn_par_iter_simple();
// warn_par_iter_simple_no_send();
warn_par_iter_simple_no_send_in_closure_body();
move_inside_closure();
// warn_par_iter_simple_into_parallel_ref_iterator();
// warn_par_iter();
warn_par_complex();
warn_par_complex_no_send()
}

fn warn_par_iter_simple() {
(0..100).into_par_iter().for_each(|x| println!("{:?}", x));
}

fn move_inside_closure() {
let y = 100;
(0..100)
.into_par_iter()
.for_each(|x| println!("{:?}{:?}", x, y));
}
// fn warn_par_iter_simple_no_send() {
// let list: Vec<Rc<i32>> = (0..100).map(Rc::new).collect();
// list.iter().for_each(|y| println!("{:?}", y));
// }

fn warn_par_iter_simple_no_send_in_closure_body() {
let mut list: Vec<Rc<i32>> = (0..100).map(Rc::new).collect();
(0..100).into_iter().for_each(|x| list.push(Rc::new(x)));
}

// fn warn_par_iter_simple_into_parallel_ref_iterator() {
// let list: LinkedList<i32> = (0..100).collect();
// list.into_iter().for_each(|x| println!("{:?}", x));
// }

struct Person {
name: String,
age: u32,
}

fn warn_par_complex() {
let vec = vec![1, 2, 3, 4, 5, 6];
let a = 10;
let b = 20;
let c = "Hello";
let d = 3.14;
let e = true;
let person = Person {
name: String::from("Alice"),
age: 30,
};

(0..10).into_par_iter().for_each(|x| {
let sum = x + a + b;
let message = if e { c } else { "Goodbye" };
let product = d * (x as f64);
let person_info = format!("{} is {} years old.", person.name, person.age);
println!(
"Sum: {sum}, Message: {message}, Product: {product}, Person: {person_info}, Vec: {vec:?}",
);
});
}

fn warn_par_complex_no_send() {
let a = Rc::new(10);
let b = Rc::new(20);
let c = Rc::new("Hello");
let d = Rc::new(3.14);
let e = Rc::new(true);
let person = Rc::new(Person {
name: String::from("Alice"),
age: 30,
});

(0..10).into_iter().for_each(|x| {
let sum = *a + *b;
let message = if *e { *c } else { "Goodbye" };
let product = *d * (x as f64);
let person_info = format!("{} is {} years old.", person.name, person.age);
println!("Sum: {sum}, Message: {message}, Product: {product}, Person: {person_info}",);
});
}

// struct LocalQueue {}

// fn warn_par_iter() {
// let thread_num = 10;
// let mut locals = Vec::new();
// (0..thread_num).into_iter().for_each(|_| {
// locals.push(LocalQueue {});
// });
// }
Loading
Loading