Skip to content

Commit f76a8d8

Browse files
committed
Implement a command to dump impls for a given type
This adds a new `--dump-impls` CLI command that can be used to dump all the recognized trait impls for a given type name. This also extends the compiler diagnostic system to allow the emission of info diagnostics, as previously we could only emit either warnings of errors, which did not fit this. Closes #7286.
1 parent ab4a67e commit f76a8d8

File tree

17 files changed

+215
-16
lines changed

17 files changed

+215
-16
lines changed

forc-pkg/src/manifest/build_profile.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use serde::{Deserialize, Serialize};
22
use sway_core::{Backtrace, OptLevel, PrintAsm, PrintIr};
33

4+
use crate::DumpOpts;
5+
46
/// Parameters to pass through to the `sway_core::BuildConfig` during compilation.
57
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
68
#[serde(rename_all = "kebab-case")]
@@ -11,6 +13,7 @@ pub struct BuildProfile {
1113
pub print_ast: bool,
1214
pub print_dca_graph: Option<String>,
1315
pub print_dca_graph_url_format: Option<String>,
16+
pub dump: DumpOpts,
1417
#[serde(default)]
1518
pub print_ir: PrintIr,
1619
#[serde(default)]
@@ -47,6 +50,7 @@ impl BuildProfile {
4750
pub fn debug() -> Self {
4851
Self {
4952
name: Self::DEBUG.into(),
53+
dump: DumpOpts::default(),
5054
print_ast: false,
5155
print_dca_graph: None,
5256
print_dca_graph_url_format: None,
@@ -69,6 +73,7 @@ impl BuildProfile {
6973
pub fn release() -> Self {
7074
Self {
7175
name: Self::RELEASE.to_string(),
76+
dump: DumpOpts::default(),
7277
print_ast: false,
7378
print_dca_graph: None,
7479
print_dca_graph_url_format: None,

forc-pkg/src/pkg.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,19 @@ pub struct MinifyOpts {
279279
/// Represents a compiled contract ID as a pub const in a contract.
280280
type ContractIdConst = String;
281281

282+
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
283+
pub struct DumpOpts {
284+
/// Dump all trait implementations for the given type name.
285+
pub dump_impls: Option<String>,
286+
}
287+
282288
/// The set of options provided to the `build` functions.
283289
#[derive(Default, Clone)]
284290
pub struct BuildOpts {
285291
pub pkg: PkgOpts,
286292
pub print: PrintOpts,
287293
pub minify: MinifyOpts,
294+
pub dump: DumpOpts,
288295
/// If set, generates a JSON file containing the hex-encoded script binary.
289296
pub hex_outfile: Option<String>,
290297
/// If set, outputs a binary file representing the script bytes.
@@ -1725,7 +1732,7 @@ pub fn compile(
17251732
&handler,
17261733
engines,
17271734
source,
1728-
namespace,
1735+
namespace.clone(),
17291736
Some(&sway_build_config),
17301737
&pkg.name,
17311738
None,
@@ -1755,6 +1762,15 @@ pub fn compile(
17551762
return fail(handler);
17561763
}
17571764

1765+
if let Some(typename) = &profile.dump.dump_impls {
1766+
let _ = sway_core::dump_trait_impls_for_typename(
1767+
&handler,
1768+
engines,
1769+
&typed_program.namespace,
1770+
typename,
1771+
);
1772+
}
1773+
17581774
let asm_res = time_expr!(
17591775
pkg.name,
17601776
"compile ast to asm",
@@ -2110,6 +2126,7 @@ fn build_profile_from_opts(
21102126
metrics_outfile,
21112127
tests,
21122128
error_on_warnings,
2129+
dump,
21132130
..
21142131
} = build_options;
21152132

@@ -2130,6 +2147,7 @@ fn build_profile_from_opts(
21302147
BuildProfile::default()
21312148
});
21322149
profile.name = selected_profile_name.into();
2150+
profile.dump = dump.clone();
21332151
profile.print_ast |= print.ast;
21342152
if profile.print_dca_graph.is_none() {
21352153
profile.print_dca_graph.clone_from(&print.dca_graph);

forc-plugins/forc-client/src/op/deploy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
},
1313
};
1414
use anyhow::{bail, Context, Result};
15-
use forc_pkg::{self as pkg, PackageManifestFile};
15+
use forc_pkg::{self as pkg, DumpOpts, PackageManifestFile};
1616
use forc_pkg::{manifest::GenericManifestFile, MemberFilter};
1717
use forc_tracing::{println_action_green, println_warning};
1818
use forc_util::default_output_directory;
@@ -814,6 +814,7 @@ fn build_opts_from_cmd(cmd: &cmd::Deploy, member_filter: pkg::MemberFilter) -> p
814814
ir: cmd.print.ir(),
815815
reverse_order: cmd.print.reverse_order,
816816
},
817+
dump: DumpOpts::default(),
817818
time_phases: cmd.print.time_phases,
818819
profile: cmd.print.profile,
819820
metrics_outfile: cmd.print.metrics_outfile.clone(),

forc-plugins/forc-client/src/op/run/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
},
99
};
1010
use anyhow::{anyhow, bail, Context, Result};
11-
use forc_pkg::{self as pkg, fuel_core_not_running, PackageManifestFile};
11+
use forc_pkg::{self as pkg, fuel_core_not_running, DumpOpts, PackageManifestFile};
1212
use forc_tracing::println_warning;
1313
use forc_util::tx_utils::format_log_receipts;
1414
use fuel_core_client::client::FuelClient;
@@ -256,6 +256,7 @@ fn build_opts_from_cmd(cmd: &cmd::Run) -> pkg::BuildOpts {
256256
json_abi: cmd.minify.json_abi,
257257
json_storage_slots: cmd.minify.json_storage_slots,
258258
},
259+
dump: DumpOpts::default(),
259260
build_target: BuildTarget::default(),
260261
build_profile: cmd.build_profile.build_profile.clone(),
261262
release: cmd.build_profile.release,

forc-test/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::setup::{
77
ContractDeploymentSetup, ContractTestSetup, DeploymentSetup, ScriptTestSetup, TestSetup,
88
};
99
use ecal::EcalSyscallHandler;
10-
use forc_pkg::{self as pkg, BuildOpts};
10+
use forc_pkg::{self as pkg, BuildOpts, DumpOpts};
1111
use forc_util::tx_utils::RevertInfo;
1212
use fuel_abi_types::abi::program::ProgramABI;
1313
use fuel_tx as tx;
@@ -452,6 +452,7 @@ impl From<TestOpts> for pkg::BuildOpts {
452452
pkg: val.pkg,
453453
print: val.print,
454454
minify: val.minify,
455+
dump: DumpOpts::default(),
455456
binary_outfile: val.binary_outfile,
456457
debug_outfile: val.debug_outfile,
457458
hex_outfile: val.hex_outfile,
@@ -477,6 +478,7 @@ impl TestOpts {
477478
pkg: self.pkg,
478479
print: self.print,
479480
minify: self.minify,
481+
dump: DumpOpts::default(),
480482
binary_outfile: self.binary_outfile,
481483
debug_outfile: self.debug_outfile,
482484
hex_outfile: self.hex_outfile,

forc/src/cli/commands/check.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub struct Command {
4545
#[clap(long)]
4646
pub ipfs_node: Option<IPFSNode>,
4747

48+
/// Dump all trait implementations for the given type name.
49+
#[clap(long = "dump-impls", value_name = "TYPE")]
50+
pub dump_impls: Option<String>,
51+
4852
#[clap(flatten)]
4953
pub experimental: sway_features::CliFields,
5054
}

forc/src/cli/shared.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ pub struct Build {
9191
/// Build target to use for code generation.
9292
#[clap(long, value_enum, default_value_t = BuildTarget::default(), alias="target")]
9393
pub build_target: BuildTarget,
94+
#[clap(flatten)]
95+
pub dump: Dump,
9496
}
9597

9698
/// Build output file options.
@@ -126,6 +128,14 @@ pub struct BuildProfile {
126128
pub error_on_warnings: bool,
127129
}
128130

131+
/// Dump options.
132+
#[derive(Args, Debug, Default)]
133+
pub struct Dump {
134+
/// Dump all trait implementations for the given type name.
135+
#[clap(long = "dump-impls", value_name = "TYPE")]
136+
pub dump_impls: Option<String>,
137+
}
138+
129139
/// Options related to printing stages of compiler output.
130140
#[derive(Args, Debug, Default)]
131141
pub struct Print {

forc/src/ops/forc_build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ fn opts_from_cmd(cmd: BuildCommand) -> pkg::BuildOpts {
2929
ir: cmd.build.print.ir(),
3030
reverse_order: cmd.build.print.reverse_order,
3131
},
32+
dump: pkg::DumpOpts {
33+
dump_impls: cmd.build.dump.dump_impls,
34+
},
3235
time_phases: cmd.build.print.time_phases,
3336
profile: cmd.build.print.profile,
3437
metrics_outfile: cmd.build.print.metrics_outfile,

forc/src/ops/forc_contract_id.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cli::ContractIdCommand;
22
use anyhow::{bail, Result};
3-
use forc_pkg::{self as pkg, build_with_options};
3+
use forc_pkg::{self as pkg, build_with_options, DumpOpts};
44
use forc_tracing::println_green;
55
use sway_core::{fuel_prelude::fuel_tx, BuildTarget};
66
use tracing::info;
@@ -63,6 +63,7 @@ fn build_opts_from_cmd(cmd: &ContractIdCommand) -> pkg::BuildOpts {
6363
ir: cmd.print.ir(),
6464
reverse_order: cmd.print.reverse_order,
6565
},
66+
dump: DumpOpts::default(),
6667
time_phases: cmd.print.time_phases,
6768
profile: cmd.print.profile,
6869
metrics_outfile: cmd.print.metrics_outfile.clone(),

forc/src/ops/forc_predicate_root.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cli::PredicateRootCommand;
22
use anyhow::Result;
3-
use forc_pkg::{self as pkg, build_with_options};
3+
use forc_pkg::{self as pkg, build_with_options, DumpOpts};
44
use sway_core::BuildTarget;
55

66
pub fn predicate_root(command: PredicateRootCommand) -> Result<()> {
@@ -32,6 +32,7 @@ fn build_opts_from_cmd(cmd: PredicateRootCommand) -> pkg::BuildOpts {
3232
ir: cmd.print.ir(),
3333
reverse_order: cmd.print.reverse_order,
3434
},
35+
dump: DumpOpts::default(),
3536
time_phases: cmd.print.time_phases,
3637
profile: cmd.print.profile,
3738
metrics_outfile: cmd.print.metrics_outfile,

0 commit comments

Comments
 (0)