Skip to content

Commit f0e4d05

Browse files
celinvaltedinski
authored andcommitted
Add support to Cargo.toml metadata tables (rust-lang#740)
Fixes rust-lang#739. Cargo rmc is able to extract flags from a toml configuration file. In order to provide a good experience to users that want to add configurations to their Cargo.toml, we are adding support to the standard metadata tables supported by cargo. We still support the [rmc] tables in cases users prefer to use a different rmc.toml file, but this is discouraged for Cargo.toml files since it will generate a warning every time a user runs cargo.
1 parent be69bc3 commit f0e4d05

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
lines changed

scripts/cargo-rmc

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,27 +275,41 @@ def parse_args():
275275
post_process(cl_args)
276276
return cl_args
277277

278-
# Extract a list of args based on given config toml
278+
279279
def extract_flags_from_toml(path):
280+
"""Extract a list of args based on given config toml
281+
RMC can be configured in the following tables:
282+
- rmc
283+
- workspace.metadata.rmc
284+
- package.metadata.rmc
285+
The only field supported today is flags.
286+
"""
280287
# Load the flag data from toml
281288
data = toml.load(path)
282289

283-
# Extract the rmc flags from the toml, if any present
284-
if ("rmc" not in data) or ("flags" not in data["rmc"]):
285-
# If no flags are present, just return none
290+
# Extract the rmc flags from the toml, if any of the supported tables are present
291+
rmc_data = dict()
292+
tables = ["workspace.metadata.rmc", "package.metadata.rmc", "rmc"]
293+
for table_name in tables:
294+
table = data
295+
for section in table_name.split("."):
296+
table = table.get(section, dict())
297+
rmc_data.update(table)
298+
299+
if "flags" not in rmc_data:
286300
return []
287-
rmc_data = data["rmc"]
301+
288302
flag_data = rmc_data["flags"]
289303

290304
# Extract nested flags
291305
flags = dict()
292306

293-
def find_flags(map):
294-
for key in map:
295-
if isinstance(map[key], dict):
296-
find_flags(map[key])
307+
def find_flags(args):
308+
for key in args:
309+
if isinstance(args[key], dict):
310+
find_flags(args[key])
297311
else:
298-
flags[key] = map[key]
312+
flags[key] = args[key]
299313
find_flags(flag_data)
300314

301315
# Add config toml flags to flag list

src/rmc-compiler/cbmc/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
//!
2525
//! Speical [id]s include:
2626
//! 1. [Empty] and [Nil] behaves like [null].
27-
#![feature(rustc_private)]
2827
2928
mod env;
3029
pub mod goto_program;
@@ -36,7 +35,5 @@ pub use machine_model::{MachineModel, RoundingMode};
3635
mod cbmc_string;
3736
pub use cbmc_string::{InternString, InternStringOption, InternedString};
3837

39-
extern crate rustc_data_structures;
40-
4138
// Rust has difficulty resolving types for None option values: this gives rustc a hint.
4239
pub const NO_PRETTY_NAME: Option<InternedString> = None;

src/rmc-compiler/rustc_codegen_rmc/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
#![feature(crate_visibility_modifier)]
66
#![feature(extern_types)]
77
#![feature(in_band_lifetimes)]
8-
#![feature(iter_zip)]
98
#![feature(nll)]
109
#![recursion_limit = "256"]
11-
#![feature(destructuring_assignment)]
1210
#![feature(box_patterns)]
1311
#![feature(once_cell)]
1412
#![feature(rustc_private)]

src/test/cargo-rmc/config/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
[package]
4+
name = "check-config"
5+
version = "0.1.0"
6+
edition = "2018"
7+
8+
[dependencies]
9+
10+
[package.metadata.rmc]
11+
flags={unwind = "5", unwinding-checks = false, function="check_config"}
12+

src/test/cargo-rmc/config/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! This testcase requires an unwind threshold of less than 10 and no-unwind-checks in order to
5+
//! succeed. These parameters are set inside Cargo.toml.
6+
7+
#[cfg(rmc)]
8+
mod rmc_tests {
9+
#[rmc::proof]
10+
fn check_config() {
11+
let mut counter = 0;
12+
while rmc::any() {
13+
counter += 1;
14+
assert!(counter < 10, "Cargo.toml should've configure rmc to stop at iteration 5");
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)