Skip to content

Commit d02dead

Browse files
Merge pull request rust-lang#22 from Chris-Hawblitzel/arc
Switch from Rc to Arc
2 parents 1af55b2 + 9469863 commit d02dead

31 files changed

+530
-526
lines changed

verify/air/src/ast.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
use std::collections::HashMap;
2-
use std::rc::Rc;
2+
use std::sync::Arc;
33

4-
pub type RawSpan = Rc<dyn std::any::Any>;
4+
pub type RawSpan = Arc<dyn std::any::Any + std::marker::Sync + std::marker::Send>;
55
#[derive(Clone)] // for Debug, see ast_util
66
pub struct Span {
77
pub description: Option<String>,
88
pub raw_span: RawSpan,
99
pub as_string: String, // if we can't print (description, raw_span), print as_string instead
1010
}
11-
pub type SpanOption = Rc<Option<Span>>;
11+
pub type SpanOption = Arc<Option<Span>>;
1212

1313
pub type TypeError = String;
1414

15-
pub type Ident = Rc<String>;
15+
pub type Ident = Arc<String>;
1616

1717
pub(crate) type Snapshot = HashMap<Ident, u32>;
1818
pub(crate) type Snapshots = HashMap<Ident, Snapshot>;
1919

20-
pub type Typ = Rc<TypX>;
21-
pub type Typs = Rc<Vec<Typ>>;
20+
pub type Typ = Arc<TypX>;
21+
pub type Typs = Arc<Vec<Typ>>;
2222
#[derive(Debug)]
2323
pub enum TypX {
2424
Bool,
@@ -29,7 +29,7 @@ pub enum TypX {
2929
#[derive(Clone, PartialEq, Eq, Hash)] // for Debug, see ast_util
3030
pub enum Constant {
3131
Bool(bool),
32-
Nat(Rc<String>),
32+
Nat(Arc<String>),
3333
}
3434

3535
#[derive(Copy, Clone, Debug)]
@@ -59,8 +59,8 @@ pub enum MultiOp {
5959
Distinct,
6060
}
6161

62-
pub type Binder<A> = Rc<BinderX<A>>;
63-
pub type Binders<A> = Rc<Vec<Binder<A>>>;
62+
pub type Binder<A> = Arc<BinderX<A>>;
63+
pub type Binders<A> = Arc<Vec<Binder<A>>>;
6464
#[derive(Clone)] // for Debug, see ast_util
6565
pub struct BinderX<A: Clone> {
6666
pub name: Ident,
@@ -74,17 +74,17 @@ pub enum Quant {
7474
}
7575

7676
pub type Trigger = Exprs;
77-
pub type Triggers = Rc<Vec<Trigger>>;
77+
pub type Triggers = Arc<Vec<Trigger>>;
7878

79-
pub type Bind = Rc<BindX>;
79+
pub type Bind = Arc<BindX>;
8080
#[derive(Clone, Debug)]
8181
pub enum BindX {
8282
Let(Binders<Expr>),
8383
Quant(Quant, Binders<Typ>, Triggers),
8484
}
8585

86-
pub type Expr = Rc<ExprX>;
87-
pub type Exprs = Rc<Vec<Expr>>;
86+
pub type Expr = Arc<ExprX>;
87+
pub type Exprs = Arc<Vec<Expr>>;
8888
#[derive(Debug)]
8989
pub enum ExprX {
9090
Const(Constant),
@@ -99,8 +99,8 @@ pub enum ExprX {
9999
LabeledAssertion(SpanOption, Expr),
100100
}
101101

102-
pub type Stmt = Rc<StmtX>;
103-
pub type Stmts = Rc<Vec<Stmt>>;
102+
pub type Stmt = Arc<StmtX>;
103+
pub type Stmts = Arc<Vec<Stmt>>;
104104
#[derive(Debug)]
105105
pub enum StmtX {
106106
Assume(Expr),
@@ -119,8 +119,8 @@ pub type Variants = Binders<Fields>;
119119
pub type Datatype = Binder<Variants>;
120120
pub type Datatypes = Binders<Variants>;
121121

122-
pub type Decl = Rc<DeclX>;
123-
pub type Decls = Rc<Vec<Decl>>;
122+
pub type Decl = Arc<DeclX>;
123+
pub type Decls = Arc<Vec<Decl>>;
124124
#[derive(Debug)]
125125
pub enum DeclX {
126126
Sort(Ident),
@@ -131,15 +131,15 @@ pub enum DeclX {
131131
Axiom(Expr),
132132
}
133133

134-
pub type Query = Rc<QueryX>;
134+
pub type Query = Arc<QueryX>;
135135
#[derive(Debug)]
136136
pub struct QueryX {
137137
pub local: Decls, // local declarations
138138
pub assertion: Stmt, // checked by SMT with global and local declarations
139139
}
140140

141-
pub type Command = Rc<CommandX>;
142-
pub type Commands = Rc<Vec<Command>>;
141+
pub type Command = Arc<CommandX>;
142+
pub type Commands = Arc<Vec<Command>>;
143143
#[derive(Debug)]
144144
pub enum CommandX {
145145
Push, // push space for temporary global declarations

verify/air/src/ast_util.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ast::{
33
Typ, TypX, UnaryOp,
44
};
55
use std::fmt::Debug;
6-
use std::rc::Rc;
6+
use std::sync::Arc;
77

88
impl Debug for Span {
99
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
@@ -29,7 +29,7 @@ impl<A: Clone> BinderX<A> {
2929
&self,
3030
f: impl FnOnce(&A) -> Result<B, E>,
3131
) -> Result<Binder<B>, E> {
32-
Ok(Rc::new(BinderX { name: self.name.clone(), a: f(&self.a)? }))
32+
Ok(Arc::new(BinderX { name: self.name.clone(), a: f(&self.a)? }))
3333
}
3434
}
3535

@@ -43,55 +43,55 @@ impl<A: Clone + Debug> std::fmt::Debug for BinderX<A> {
4343
}
4444

4545
pub fn str_ident(x: &str) -> Ident {
46-
Rc::new(x.to_string())
46+
Arc::new(x.to_string())
4747
}
4848

4949
pub fn ident_var(x: &Ident) -> Expr {
50-
Rc::new(ExprX::Var(x.clone()))
50+
Arc::new(ExprX::Var(x.clone()))
5151
}
5252

5353
pub fn string_var(x: &String) -> Expr {
54-
Rc::new(ExprX::Var(Rc::new(x.clone())))
54+
Arc::new(ExprX::Var(Arc::new(x.clone())))
5555
}
5656

5757
pub fn str_var(x: &str) -> Expr {
58-
Rc::new(ExprX::Var(Rc::new(x.to_string())))
58+
Arc::new(ExprX::Var(Arc::new(x.to_string())))
5959
}
6060

6161
pub fn ident_apply(x: &Ident, args: &Vec<Expr>) -> Expr {
62-
Rc::new(ExprX::Apply(x.clone(), Rc::new(args.clone())))
62+
Arc::new(ExprX::Apply(x.clone(), Arc::new(args.clone())))
6363
}
6464

6565
pub fn string_apply(x: &String, args: &Vec<Expr>) -> Expr {
66-
Rc::new(ExprX::Apply(Rc::new(x.clone()), Rc::new(args.clone())))
66+
Arc::new(ExprX::Apply(Arc::new(x.clone()), Arc::new(args.clone())))
6767
}
6868

6969
pub fn str_apply(x: &str, args: &Vec<Expr>) -> Expr {
70-
Rc::new(ExprX::Apply(Rc::new(x.to_string()), Rc::new(args.clone())))
70+
Arc::new(ExprX::Apply(Arc::new(x.to_string()), Arc::new(args.clone())))
7171
}
7272

7373
pub fn int_typ() -> Typ {
74-
Rc::new(TypX::Int)
74+
Arc::new(TypX::Int)
7575
}
7676

7777
pub fn bool_typ() -> Typ {
78-
Rc::new(TypX::Bool)
78+
Arc::new(TypX::Bool)
7979
}
8080

8181
pub fn ident_typ(x: &Ident) -> Typ {
82-
Rc::new(TypX::Named(x.clone()))
82+
Arc::new(TypX::Named(x.clone()))
8383
}
8484

8585
pub fn string_typ(x: &String) -> Typ {
86-
Rc::new(TypX::Named(Rc::new(x.clone())))
86+
Arc::new(TypX::Named(Arc::new(x.clone())))
8787
}
8888

8989
pub fn str_typ(x: &str) -> Typ {
90-
Rc::new(TypX::Named(Rc::new(x.to_string())))
90+
Arc::new(TypX::Named(Arc::new(x.to_string())))
9191
}
9292

9393
pub fn ident_binder<A: Clone>(x: &Ident, a: &A) -> Binder<A> {
94-
Rc::new(BinderX { name: x.clone(), a: a.clone() })
94+
Arc::new(BinderX { name: x.clone(), a: a.clone() })
9595
}
9696

9797
pub fn mk_quantifier(
@@ -100,8 +100,8 @@ pub fn mk_quantifier(
100100
triggers: &Vec<Trigger>,
101101
body: &Expr,
102102
) -> Expr {
103-
Rc::new(ExprX::Bind(
104-
Rc::new(BindX::Quant(quant, Rc::new(binders.clone()), Rc::new(triggers.clone()))),
103+
Arc::new(ExprX::Bind(
104+
Arc::new(BindX::Quant(quant, Arc::new(binders.clone()), Arc::new(triggers.clone()))),
105105
body.clone(),
106106
))
107107
}
@@ -115,11 +115,11 @@ pub fn mk_exists(binders: &Vec<Binder<Typ>>, triggers: &Vec<Trigger>, body: &Exp
115115
}
116116

117117
pub fn mk_true() -> Expr {
118-
Rc::new(ExprX::Const(Constant::Bool(true)))
118+
Arc::new(ExprX::Const(Constant::Bool(true)))
119119
}
120120

121121
pub fn mk_false() -> Expr {
122-
Rc::new(ExprX::Const(Constant::Bool(false)))
122+
Arc::new(ExprX::Const(Constant::Bool(false)))
123123
}
124124

125125
pub fn mk_and(exprs: &Vec<Expr>) -> Expr {
@@ -136,7 +136,7 @@ pub fn mk_and(exprs: &Vec<Expr>) -> Expr {
136136
} else if exprs.len() == 1 {
137137
exprs[0].clone()
138138
} else {
139-
Rc::new(ExprX::Multi(MultiOp::And, Rc::new(exprs)))
139+
Arc::new(ExprX::Multi(MultiOp::And, Arc::new(exprs)))
140140
}
141141
}
142142

@@ -154,7 +154,7 @@ pub fn mk_or(exprs: &Vec<Expr>) -> Expr {
154154
} else if exprs.len() == 1 {
155155
exprs[0].clone()
156156
} else {
157-
Rc::new(ExprX::Multi(MultiOp::Or, Rc::new(exprs)))
157+
Arc::new(ExprX::Multi(MultiOp::Or, Arc::new(exprs)))
158158
}
159159
}
160160

@@ -163,7 +163,7 @@ pub fn mk_not(e1: &Expr) -> Expr {
163163
ExprX::Const(Constant::Bool(false)) => mk_true(),
164164
ExprX::Const(Constant::Bool(true)) => mk_false(),
165165
ExprX::Unary(UnaryOp::Not, e) => e.clone(),
166-
_ => Rc::new(ExprX::Unary(UnaryOp::Not, e1.clone())),
166+
_ => Arc::new(ExprX::Unary(UnaryOp::Not, e1.clone())),
167167
}
168168
}
169169

@@ -173,7 +173,7 @@ pub fn mk_implies(e1: &Expr, e2: &Expr) -> Expr {
173173
(ExprX::Const(Constant::Bool(true)), _) => e2.clone(),
174174
(_, ExprX::Const(Constant::Bool(false))) => mk_not(e1),
175175
(_, ExprX::Const(Constant::Bool(true))) => mk_true(),
176-
_ => Rc::new(ExprX::Binary(BinaryOp::Implies, e1.clone(), e2.clone())),
176+
_ => Arc::new(ExprX::Binary(BinaryOp::Implies, e1.clone(), e2.clone())),
177177
}
178178
}
179179

@@ -185,10 +185,10 @@ pub fn mk_ite(e1: &Expr, e2: &Expr, e3: &Expr) -> Expr {
185185
(_, _, ExprX::Const(Constant::Bool(false))) => mk_and(&vec![e1.clone(), e2.clone()]),
186186
(_, ExprX::Const(Constant::Bool(true)), _) => mk_implies(&mk_not(e1), e3),
187187
(_, ExprX::Const(Constant::Bool(false)), _) => mk_and(&vec![mk_not(e1), e3.clone()]),
188-
_ => Rc::new(ExprX::IfElse(e1.clone(), e2.clone(), e3.clone())),
188+
_ => Arc::new(ExprX::IfElse(e1.clone(), e2.clone(), e3.clone())),
189189
}
190190
}
191191

192192
pub fn mk_eq(e1: &Expr, e2: &Expr) -> Expr {
193-
Rc::new(ExprX::Binary(BinaryOp::Eq, e1.clone(), e2.clone()))
193+
Arc::new(ExprX::Binary(BinaryOp::Eq, e1.clone(), e2.clone()))
194194
}

verify/air/src/block_to_assert.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ use crate::ast::{
33
};
44
use crate::ast_util::bool_typ;
55
use crate::def::SWITCH_LABEL;
6-
use std::rc::Rc;
6+
use std::sync::Arc;
77

88
fn stmt_to_expr(label_n: &mut u64, locals: &mut Vec<Decl>, stmt: &Stmt, pred: Expr) -> Expr {
99
match &**stmt {
1010
StmtX::Assume(expr) => {
1111
// wp((assume Q), P) = Q ==> P
12-
Rc::new(ExprX::Binary(BinaryOp::Implies, expr.clone(), pred))
12+
Arc::new(ExprX::Binary(BinaryOp::Implies, expr.clone(), pred))
1313
}
1414
StmtX::Assert(span, expr) => {
1515
// wp((assert Q), P) = Q /\ P
16-
let assertion = Rc::new(ExprX::LabeledAssertion(span.clone(), expr.clone()));
17-
Rc::new(ExprX::Multi(MultiOp::And, Rc::new(vec![assertion, pred])))
16+
let assertion = Arc::new(ExprX::LabeledAssertion(span.clone(), expr.clone()));
17+
Arc::new(ExprX::Multi(MultiOp::And, Arc::new(vec![assertion, pred])))
1818
}
1919
StmtX::Havoc(_) => panic!("internal error: Havoc in block_to_assert"),
2020
StmtX::Assign(_, _) => panic!("internal error: Assign in block_to_assert"),
@@ -32,27 +32,27 @@ fn stmt_to_expr(label_n: &mut u64, locals: &mut Vec<Decl>, stmt: &Stmt, pred: Ex
3232
// To avoid duplicating P, we use:
3333
// wp((s1 or s2), P) = (P ==> label) ==> wp(s1, label) /\ wp(s2, label)
3434
// = (wp(s1, label) /\ wp(s2, label)) \/ (!label /\ P)
35-
let label = Rc::new(format!("{}{}", SWITCH_LABEL, label_n));
35+
let label = Arc::new(format!("{}{}", SWITCH_LABEL, label_n));
3636
*label_n += 1;
37-
locals.push(Rc::new(DeclX::Const(label.clone(), bool_typ())));
38-
let exp_label = Rc::new(ExprX::Var(label));
37+
locals.push(Arc::new(DeclX::Const(label.clone(), bool_typ())));
38+
let exp_label = Arc::new(ExprX::Var(label));
3939
let mut exprs: Vec<Expr> = Vec::new();
4040
for stmt in stmts.iter() {
4141
exprs.push(stmt_to_expr(label_n, locals, stmt, exp_label.clone()));
4242
}
43-
let neg_label = Rc::new(ExprX::Unary(UnaryOp::Not, exp_label));
44-
let and1 = Rc::new(ExprX::Multi(MultiOp::And, Rc::new(exprs)));
45-
let and2 = Rc::new(ExprX::Multi(MultiOp::And, Rc::new(vec![neg_label, pred])));
46-
Rc::new(ExprX::Multi(MultiOp::Or, Rc::new(vec![and1, and2])))
43+
let neg_label = Arc::new(ExprX::Unary(UnaryOp::Not, exp_label));
44+
let and1 = Arc::new(ExprX::Multi(MultiOp::And, Arc::new(exprs)));
45+
let and2 = Arc::new(ExprX::Multi(MultiOp::And, Arc::new(vec![neg_label, pred])));
46+
Arc::new(ExprX::Multi(MultiOp::Or, Arc::new(vec![and1, and2])))
4747
}
4848
}
4949
}
5050

5151
pub(crate) fn lower_query(query: &Query) -> Query {
5252
let mut locals: Vec<Decl> = (*query.local).clone();
5353
let mut switch_label: u64 = 0;
54-
let tru = Rc::new(ExprX::Const(Constant::Bool(true)));
54+
let tru = Arc::new(ExprX::Const(Constant::Bool(true)));
5555
let expr = stmt_to_expr(&mut switch_label, &mut locals, &query.assertion, tru);
56-
let assertion = Rc::new(StmtX::Assert(Rc::new(None), expr));
57-
Rc::new(QueryX { local: Rc::new(locals), assertion })
56+
let assertion = Arc::new(StmtX::Assert(Arc::new(None), expr));
57+
Arc::new(QueryX { local: Arc::new(locals), assertion })
5858
}

verify/air/src/context.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::model::Model;
33
use crate::print_parse::Logger;
44
use crate::typecheck::Typing;
55
use std::collections::{HashMap, HashSet};
6-
use std::rc::Rc;
6+
use std::sync::Arc;
77
use z3::ast::Dynamic;
88
use z3::{FuncDecl, Sort};
99

@@ -24,10 +24,10 @@ pub enum ValidityResult<'a> {
2424
pub struct Context<'ctx> {
2525
pub(crate) context: &'ctx z3::Context,
2626
pub(crate) solver: &'ctx z3::Solver<'ctx>,
27-
pub(crate) typs: HashMap<Ident, Rc<Sort<'ctx>>>,
28-
pub(crate) vars: HashMap<Ident, Dynamic<'ctx>>, // no Rc; Dynamic implements Clone
29-
pub(crate) funs: HashMap<Ident, Rc<FuncDecl<'ctx>>>,
30-
pub(crate) assert_infos: HashMap<Ident, Rc<AssertionInfo>>,
27+
pub(crate) typs: HashMap<Ident, Arc<Sort<'ctx>>>,
28+
pub(crate) vars: HashMap<Ident, Dynamic<'ctx>>, // no Arc; Dynamic implements Clone
29+
pub(crate) funs: HashMap<Ident, Arc<FuncDecl<'ctx>>>,
30+
pub(crate) assert_infos: HashMap<Ident, Arc<AssertionInfo>>,
3131
pub(crate) assert_infos_count: u64,
3232
pub(crate) typing: Typing,
3333
pub(crate) debug: bool,

verify/air/src/model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a> Model<'a> {
7979
let var_smt = new_const(context, &var_name, &typ);
8080
let val = self.lookup_z3_var(&var_name, &var_smt);
8181
value_snapshot.insert(var_name.clone(), val);
82-
//value_snapshot.insert(Rc::new((*var_name).clone()), val);
82+
//value_snapshot.insert(Arc::new((*var_name).clone()), val);
8383
} else {
8484
panic!("Expected local vars to all be constants at this point");
8585
}

0 commit comments

Comments
 (0)