Skip to content
Open
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
91 changes: 91 additions & 0 deletions libs/pavexc/src/compiler/analyses/cloning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,94 @@ fn must_be_clonable(
.build();
diagnostics.push(diagnostic);
}

/// Check all types whose cloning strategy is set to "NeverClone" are not Copy.

#[tracing::instrument(
name = "If cloning is not allowed, types should not be copyable",
skip_all
)]

pub(crate) fn never_clones_should_not_be_copyable<'a>(
component_db: &ComponentDb,
computation_db: &ComputationDb,
krate_collection: &CrateCollection,
diagnostics: &mut crate::diagnostic::DiagnosticSink,
) {
let copy = process_framework_path("core::marker::Copy", krate_collection);

let ResolvedType::ResolvedPath(copy) = copy else {
unreachable!()
};

for (id, _) in component_db.iter() {
let hydrated = component_db.hydrated_component(id, computation_db);
match hydrated {
HydratedComponent::Constructor(_) | HydratedComponent::PrebuiltType(_) => {
if component_db.cloning_strategy(id) != CloningStrategy::NeverClone {
continue;
}
}
_ => {
continue;
}
};

let Some(output_type) = hydrated.output_type() else {
continue;
};

if let Ok(()) = assert_trait_is_implemented(krate_collection, output_type, &copy) {
should_not_be_never_clone(output_type, id, component_db, computation_db, diagnostics);
}
}
}

fn should_not_be_never_clone(
type_: &ResolvedType,
id: ComponentId,
db: &ComponentDb,
computation_db: &ComputationDb,
diagnostics: &mut crate::diagnostic::DiagnosticSink,
) {
let id = db.derived_from(&id).unwrap_or(id);
let user_id = db.user_component_id(id).unwrap();
let kind = db.user_db()[user_id].kind();
let registration = db.registration(user_id);
let source = diagnostics.annotated(
db.registration_target(user_id),
format!("The {kind} was registered here"),
);

let (clone_if_necessary, never_clone) = if registration.kind.is_attribute() {
("clone_if_necessary", "never_clone")
} else {
("CloneIfNecessary", "NeverClone")
};

let output_type = type_.display_for_error();
let warning_msg = match kind {
ComponentKind::Constructor => {
let callable_path = &computation_db[user_id].path;
format!(
"`{output_type}` implements `Copy`, but its constructor, `{callable_path}`, is marked as `{never_clone}`."
)
}
ComponentKind::PrebuiltType => {
format!("`{output_type}` implements `Copy`, but it's marked as `{never_clone}`.")
}
_ => unreachable!(),
};

let help = format!(
"Either set the cloning strategy to `{clone_if_necessary}` or remove `Copy` for `{output_type}`",
);

let diagnostic = CompilerDiagnostic::builder(anyhow::anyhow!(warning_msg))
.severity(miette::Severity::Warning)
.optional_source(source)
.help(help)
.build();

diagnostics.push(diagnostic);
}
10 changes: 9 additions & 1 deletion libs/pavexc/src/compiler/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use crate::compiler::analyses::application_state::ApplicationState;
use crate::compiler::analyses::call_graph::{
ApplicationStateCallGraph, application_state_call_graph,
};
use crate::compiler::analyses::cloning::clonables_can_be_cloned;
use crate::compiler::analyses::cloning::{
clonables_can_be_cloned, never_clones_should_not_be_copyable,
};
use crate::compiler::analyses::components::{ComponentDb, ComponentId};
use crate::compiler::analyses::computations::ComputationDb;
use crate::compiler::analyses::config_types::ConfigTypeDb;
Expand Down Expand Up @@ -116,6 +118,12 @@ impl App {
&krate_collection,
&mut diagnostics,
);
never_clones_should_not_be_copyable(
&component_db,
&computation_db,
&krate_collection,
&mut diagnostics,
);
exit_on_errors!(diagnostics);
let handler_id2pipeline = {
let handler_ids: BTreeSet<_> = router.handler_ids();
Expand Down
2 changes: 2 additions & 0 deletions libs/ui_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ members = [
"annotations/non_existing_dependency/generated_app",
"app_builder",
"app_builder/generated_app",
"blueprint/common/a_warning_is_emitted_for_copyable_never_clone",
"blueprint/common/a_warning_is_emitted_for_copyable_never_clone/generated_app",
"blueprint/common/async_callable_are_supported",
"blueprint/common/async_callable_are_supported/generated_app",
"blueprint/common/cannot_return_the_unit_type",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "app_cd2e094a"
version = "0.1.0"
edition.workspace = true

[lints.rust.unexpected_cfgs]
level = "allow"
check-cfg = ["cfg(pavex_ide_hint)"]

[dependencies]
workspace_hack = { version = "0.1", path = "../../../workspace_hack" }

[dependencies.pavex]
workspace = true

[dependencies.pavex_cli_client]
workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
digraph "GET / - 0" {
0 [ label = "0| app_cd2e094a::B"]
1 [ label = "1| crate::route_0::Next0(app_cd2e094a::B) -> crate::route_0::Next0"]
2 [ label = "2| pavex::middleware::Next::new(crate::route_0::Next0) -> pavex::middleware::Next<crate::route_0::Next0>"]
3 [ label = "3| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0>) -> pavex::response::Response"]
4 [ label = "4| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
2 -> 3 [ ]
1 -> 2 [ ]
0 -> 1 [ ]
3 -> 4 [ ]
}

digraph "GET / - 1" {
0 [ label = "0| app_cd2e094a::B"]
1 [ label = "1| app_cd2e094a::a() -> app_cd2e094a::A"]
2 [ label = "2| app_cd2e094a::handler_1(app_cd2e094a::A, app_cd2e094a::B) -> pavex::response::Response"]
3 [ label = "3| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
0 -> 2 [ ]
1 -> 2 [ ]
2 -> 3 [ ]
}

digraph "GET /2 - 0" {
0 [ label = "0| app_cd2e094a::A1"]
1 [ label = "1| app_cd2e094a::B1"]
2 [ label = "2| crate::route_1::Next0(app_cd2e094a::B1, app_cd2e094a::A1) -> crate::route_1::Next0"]
3 [ label = "3| pavex::middleware::Next::new(crate::route_1::Next0) -> pavex::middleware::Next<crate::route_1::Next0>"]
4 [ label = "4| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_1::Next0>) -> pavex::response::Response"]
5 [ label = "5| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
3 -> 4 [ ]
2 -> 3 [ ]
0 -> 2 [ ]
1 -> 2 [ ]
4 -> 5 [ ]
}

digraph "GET /2 - 1" {
0 [ label = "0| app_cd2e094a::B1"]
1 [ label = "1| app_cd2e094a::A1"]
2 [ label = "2| app_cd2e094a::handler_2(app_cd2e094a::A1, app_cd2e094a::B1) -> pavex::response::Response"]
3 [ label = "3| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
0 -> 2 [ ]
1 -> 2 [ ]
2 -> 3 [ ]
}

digraph "* * - 0" {
0 [ label = "0| &pavex::router::AllowedMethods"]
1 [ label = "1| crate::route_2::Next0(&'a pavex::router::AllowedMethods) -> crate::route_2::Next0<'a>"]
2 [ label = "2| pavex::middleware::Next::new(crate::route_2::Next0<'a>) -> pavex::middleware::Next<crate::route_2::Next0<'a>>"]
3 [ label = "3| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_2::Next0<'a>>) -> pavex::response::Response"]
4 [ label = "4| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
2 -> 3 [ ]
1 -> 2 [ ]
3 -> 4 [ ]
0 -> 1 [ ]
}

digraph "* * - 1" {
0 [ label = "0| &pavex::router::AllowedMethods"]
1 [ label = "1| pavex::router::default_fallback(&pavex::router::AllowedMethods) -> pavex::response::Response"]
2 [ label = "2| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
1 -> 2 [ ]
0 -> 1 [ ]
}

digraph app_state {
0 [ label = "0| app_cd2e094a::b1() -> app_cd2e094a::B1"]
1 [ label = "1| app_cd2e094a::b() -> app_cd2e094a::B"]
2 [ label = "2| app_cd2e094a::a1() -> app_cd2e094a::A1"]
3 [ label = "3| crate::ApplicationState(app_cd2e094a::A1, app_cd2e094a::B, app_cd2e094a::B1) -> crate::ApplicationState"]
0 -> 3 [ ]
1 -> 3 [ ]
2 -> 3 [ ]
}
Loading
Loading