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

Fix unwrap error when prusti-contracts compiled with stable #1173

Merged
merged 3 commits into from
Oct 3, 2022
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion analysis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "analysis"
version = "0.1.0"
authors = ["Federico Poli <federpoli@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion jni-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "jni-gen"
version = "0.1.0"
description = "Generator of Rust bindings to Java classes"
authors = ["Federico Poli <federpoli@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
license = "MPL-2.0"
readme = "README.md"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion prusti-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "prusti-common"
version = "0.1.0"
authors = ["Julian Dunskus <julian.dunskus@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion prusti-contracts-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "prusti-contracts-build"
version = "0.1.0"
authors = ["Vytautas Astrauskas <vastrauskas@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[build-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions prusti-contracts/prusti-contracts-proc-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prusti-contracts-proc-macros"
version = "0.1.1"
version = "0.1.2"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
license = "MPL-2.0"
Expand All @@ -15,7 +15,7 @@ categories = ["development-tools", "development-tools::testing"]
proc-macro = true

[dependencies]
prusti-specs = { path = "../prusti-specs", version = "0.1.1", optional = true }
prusti-specs = { path = "../prusti-specs", version = "0.1.2", optional = true }
proc-macro2 = { version = "1.0", optional = true }

[features]
Expand Down
4 changes: 2 additions & 2 deletions prusti-contracts/prusti-contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prusti-contracts"
version = "0.1.1"
version = "0.1.2"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
license = "MPL-2.0"
Expand All @@ -12,7 +12,7 @@ keywords = ["prusti", "contracts", "verification", "formal", "specifications"]
categories = ["development-tools", "development-tools::testing"]

[dependencies]
prusti-contracts-proc-macros = { path = "../prusti-contracts-proc-macros", version = "0.1.1" }
prusti-contracts-proc-macros = { path = "../prusti-contracts-proc-macros", version = "0.1.2" }

[dev-dependencies]
trybuild = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion prusti-contracts/prusti-specs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prusti-specs"
version = "0.1.1"
version = "0.1.2"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
license = "MPL-2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ fn rewrite_extern_spec_internal(item_impl: &syn::ItemImpl) -> syn::Result<Rewrit
#struct_ident #generic_args
};

if item_impl.trait_.is_some() {
let (_, trait_path, _) = item_impl.trait_.as_ref().unwrap();
if let Some((_, trait_path, _)) = &item_impl.trait_ {
if has_generic_arguments(trait_path) {
return Err(syn::Error::new(
item_impl.generics.params.span(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ fn generate_new_struct(item_trait: &syn::ItemTrait) -> syn::Result<GeneratedStru
};

// Add a where clause which restricts this self type parameter to the trait
if item_trait.generics.where_clause.as_ref().is_some() {
let span = item_trait.generics.where_clause.as_ref().unwrap().span();
return Err(syn::Error::new(span, "Where clauses for extern traits specs are not supported"));
if let Some(where_clause) = &item_trait.generics.where_clause {
return Err(syn::Error::new(where_clause.span(), "Where clauses for extern traits specs are not supported"));
}
let self_where_clause: syn::WhereClause = parse_quote! {
where #self_type_ident: #self_type_trait
Expand Down Expand Up @@ -140,9 +139,9 @@ impl<'a> GeneratedStruct<'a> {
));
}
syn::TraitItem::Method(trait_method) => {
if trait_method.default.is_some() {
if let Some(default) = &trait_method.default {
return Err(syn::Error::new(
trait_method.default.as_ref().unwrap().span(),
default.span(),
"Default methods in external trait specs are invalid",
));
}
Expand Down
38 changes: 23 additions & 15 deletions prusti-contracts/prusti-specs/src/specifications/preparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,10 @@ impl PrustiTokenStream {
{
let result = f(&mut self)?;
if !self.is_empty() {
let start = self.tokens.front().unwrap().span();
let end = self.tokens.back().unwrap().span();
let span = start.join(end);
// this is None if the spans are not combinable, which seems to happen when running the cfg(tests) via cargo
let error_span = span.unwrap_or(start);
return err(error_span, "unexpected extra tokens");
let start = self.tokens.front().expect("unreachable").span();
let end = self.tokens.back().expect("unreachable").span();
let span = join_spans(start, end);
return err(span, "unexpected extra tokens");
}
Ok(result)
}
Expand Down Expand Up @@ -227,12 +225,10 @@ impl PrustiTokenStream {
TokenStream
)> {
let mut pledge_ops = self.split(PrustiBinaryOp::Rust(RustOp::Arrow), false);
let (reference, body) = if pledge_ops.len() == 1 {
(None, pledge_ops.pop().unwrap())
} else if pledge_ops.len() == 2 {
(Some(pledge_ops[0].expr_bp(0)?), pledge_ops.pop().unwrap())
} else {
return err(Span::call_site(), "too many arrows in assert_on_expiry");
let (reference, body) = match (pledge_ops.pop(), pledge_ops.pop(), pledge_ops.pop()) {
(Some(body), None, _) => (None, body),
(Some(body), Some(mut reference), None) => (Some(reference.expr_bp(0)?), body),
_ => return err(Span::call_site(), "too many arrows in assert_on_expiry"),
};
let mut body_parts = body.split(PrustiBinaryOp::Rust(RustOp::Comma), false);
if body_parts.len() == 2 {
Expand Down Expand Up @@ -507,7 +503,7 @@ impl Parse for GhostConstraint {
Ok(GhostConstraint {
trait_bounds: parse_trait_bounds(input)?,
comma: input.parse().map_err(with_ghost_constraint_example)?,
specs: PrustiTokenStream::new(input.parse().unwrap())
specs: PrustiTokenStream::new(input.parse().expect("Failed to parse GhostConstraint"))
.parse_rest(|pts| pts.pop_group_of_nested_specs(input.span()))?,
})
}
Expand Down Expand Up @@ -745,7 +741,7 @@ impl PrustiToken {
p1: &Punct,
p2: &Punct,
) -> Option<Self> {
let span = p1.span().join(p2.span()).unwrap();
let span = join_spans(p1.span(), p2.span());
Some(Self::BinOp(span, if operator2("&&", p1, p2) {
PrustiBinaryOp::And
} else if operator2("||", p1, p2) {
Expand Down Expand Up @@ -786,7 +782,7 @@ impl PrustiToken {
p2: &Punct,
p3: &Punct,
) -> Option<Self> {
let span = p1.span().join(p2.span()).unwrap().join(p3.span()).unwrap();
let span = join_spans(join_spans(p1.span(), p2.span()), p3.span());
Some(Self::BinOp(span, if operator3("==>", p1, p2, p3) {
PrustiBinaryOp::Implies
} else if operator3("===", p1, p2, p3) {
Expand Down Expand Up @@ -908,6 +904,18 @@ impl RustOp {
}
}

fn join_spans(s1: Span, s2: Span) -> Span {
// Tests don't run in the proc macro context
let is_proc_macro = std::panic::catch_unwind(|| s1.unwrap()).is_ok();
if is_proc_macro {
// This works even when compiled with stable
s1.unwrap().join(s2.unwrap()).expect("Failed to join spans!").into()
} else {
// During tests we don't care so much about returning a default
s1.join(s2).unwrap_or(s1)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions prusti-contracts/prusti-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prusti-std"
version = "0.1.1"
version = "0.1.2"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
license = "MPL-2.0"
Expand All @@ -12,7 +12,7 @@ keywords = ["prusti", "contracts", "verification", "formal", "specifications"]
categories = ["development-tools", "development-tools::testing"]

[dependencies]
prusti-contracts = { path = "../prusti-contracts", version = "0.1.1" }
prusti-contracts = { path = "../prusti-contracts", version = "0.1.2" }

# Forward "prusti" flag
[features]
Expand Down
2 changes: 1 addition & 1 deletion prusti-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "prusti-interface"
version = "0.1.0"
authors = ["Federico Poli <federpoli@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
description = "Interface between prusti and prusti-viper"
license = "MPL-2.0"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion prusti-launch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "prusti-launch"
version = "0.1.0"
authors = ["Julian Dunskus <julian.dunskus@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[[bin]]
Expand Down
1 change: 1 addition & 0 deletions prusti-rustc-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "prusti-rustc-interface"
version = "0.1.0"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions prusti-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
authors = ["Julian Dunskus <julian.dunskus@gmail.com>"]
description = "Server-side logic & handling for Prusti"
name = "prusti-server"
license = "MPL-2.0"
version = "0.1.0"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
license = "MPL-2.0"
description = "Server-side logic & handling for Prusti"

[lib]
test = true # we have unit tests
Expand Down
1 change: 1 addition & 0 deletions prusti-smt-solver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "prusti-smt-solver"
version = "0.1.0"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion prusti-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "prusti-tests"
version = "0.2.0"
authors = ["Federico Poli <federpoli@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion prusti-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "prusti-utils"
version = "0.1.0"
authors = ["Julian Dunskus <julian.dunskus@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[lib]
Expand Down
8 changes: 4 additions & 4 deletions prusti-viper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
authors = ["Federico Poli <federpoli@gmail.com>"]
description = "Translation of MIR into Viper"
name = "prusti-viper"
readme = "README.md"
license = "MPL-2.0"
version = "0.1.0"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
description = "Translation of MIR into Viper"
readme = "README.md"
license = "MPL-2.0"

[lib]
doctest = false # we have no doc tests
Expand Down
2 changes: 1 addition & 1 deletion prusti/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "prusti"
version = "0.2.1"
authors = ["Vytautas Astrauskas <vastrauskas@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[[bin]]
Expand Down
1 change: 1 addition & 0 deletions smt-log-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "smt-log-analyzer"
version = "0.1.0"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion test-crates/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "test-crates"
version = "0.1.0"
authors = ["Federico Poli <federpoli@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions viper-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "viper-sys"
version = "0.1.0"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
description = "Low-level interface to Viper"
authors = ["Federico Poli <federpoli@gmail.com>"]
license = "MPL-2.0"
readme = "README.md"
license = "MPL-2.0"
build = "build.rs"
edition = "2021"

[build-dependencies]
jni-gen = { path = "../jni-gen" }
Expand Down
6 changes: 3 additions & 3 deletions viper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "viper"
version = "0.1.0"
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
description = "High-level interface to Viper"
authors = ["Federico Poli <federpoli@gmail.com>"]
license = "MPL-2.0"
readme = "README.md"
edition = "2021"
license = "MPL-2.0"

[dependencies]
log = { version = "0.4", features = ["release_max_level_info"] }
Expand Down
2 changes: 1 addition & 1 deletion vir-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "vir-gen"
version = "0.1.0"
authors = ["Vytautas Astrauskas <vastrauskas@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"
license = "MIT OR Apache-2.0"
description = "A VIR generator."
Expand Down
2 changes: 1 addition & 1 deletion vir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "vir"
version = "0.1.0"
authors = ["Vytautas Astrauskas <vastrauskas@gmail.com>"]
authors = ["Prusti Devs <prusti_developers@sympa.ethz.ch>"]
edition = "2021"

[dependencies]
Expand Down