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

Check that trait references are for the correct trait #527

Merged
merged 6 commits into from
Jan 17, 2025
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
6 changes: 3 additions & 3 deletions charon/Cargo.lock

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

3 changes: 2 additions & 1 deletion charon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ tracing = { version = "0.1", features = [ "max_level_trace" ] }
wait-timeout = { version = "0.2.0", optional = true }
which = "7.0"

hax-frontend-exporter = { git = "https://github.com/cryspen/hax", branch = "main", optional = true }
# hax-frontend-exporter = { git = "https://github.com/cryspen/hax", branch = "main", optional = true }
hax-frontend-exporter = { git = "https://github.com/Nadrieril/hax", branch = "fix-clause-order", optional = true }
# hax-frontend-exporter = { path = "../../hax/frontend/exporter", optional = true }
macros = { path = "./macros" }

Expand Down
134 changes: 64 additions & 70 deletions charon/src/pretty/fmt_with_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
use itertools::Itertools;
use std::{
borrow::Cow,
fmt::{self, Display, Write},
fmt::{self, Debug, Display, Write},
};

/// Format the AST type as a string.
Expand Down Expand Up @@ -185,6 +185,13 @@ impl<C: AstFormatter> FmtWithCtx<C> for ConstGeneric {
}
}

impl<C: AstFormatter> FmtWithCtx<C> for ConstGenericVar {
fn fmt_with_ctx(&self, ctx: &C) -> String {
let ty = self.ty.fmt_with_ctx(ctx);
format!("const {} : {}", self.name, ty)
}
}

impl<C: AstFormatter> FmtWithCtx<C> for DeclarationGroup {
fn fmt_with_ctx(&self, ctx: &C) -> String {
use DeclarationGroup::*;
Expand Down Expand Up @@ -369,9 +376,9 @@ impl GenericParams {
where
C: AstFormatter,
{
let regions = self.regions.iter().map(|x| ctx.format_object(x));
let types = self.types.iter().map(|x| x.to_string());
let const_generics = self.const_generics.iter().map(|x| x.to_string());
let regions = self.regions.iter().map(|x| x.fmt_with_ctx(ctx));
let types = self.types.iter().map(|x| x.fmt_with_ctx(ctx));
let const_generics = self.const_generics.iter().map(|x| x.fmt_with_ctx(ctx));
regions.chain(types).chain(const_generics)
}

Expand Down Expand Up @@ -606,6 +613,17 @@ impl<C: AstFormatter> FmtWithCtx<C> for ImplElem {
}
}

impl<C: AstFormatter> FmtWithCtx<C> for LiteralTy {
fn fmt_with_ctx(&self, _ctx: &C) -> String {
match self {
LiteralTy::Integer(ty) => ty.to_string(),
LiteralTy::Float(ty) => ty.to_string(),
LiteralTy::Char => "char".to_owned(),
LiteralTy::Bool => "bool".to_owned(),
}
}
}

impl<C: AstFormatter> FmtWithCtx<C> for Name {
fn fmt_with_ctx(&self, ctx: &C) -> String {
let name = self
Expand Down Expand Up @@ -828,6 +846,12 @@ impl<T> RegionBinder<T> {
}
}

impl<C: AstFormatter> FmtWithCtx<C> for RegionVar {
fn fmt_with_ctx(&self, ctx: &C) -> String {
ctx.format_object(self)
}
}

impl<C: AstFormatter> FmtWithCtx<C> for Rvalue {
fn fmt_with_ctx(&self, ctx: &C) -> String {
match self {
Expand Down Expand Up @@ -1444,6 +1468,12 @@ impl<C: AstFormatter> FmtWithCtx<C> for TypeId {
}
}

impl<C: AstFormatter> FmtWithCtx<C> for TypeVar {
fn fmt_with_ctx(&self, _ctx: &C) -> String {
self.name.to_string()
}
}

impl<C: AstFormatter> FmtWithCtx<C> for UnOp {
fn fmt_with_ctx(&self, ctx: &C) -> String {
match self {
Expand Down Expand Up @@ -1596,30 +1626,12 @@ impl std::fmt::Display for IntegerTy {
}
}

impl std::fmt::Display for GenericArgs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self.fmt_with_ctx(&FmtCtx::new()))
}
}

impl std::fmt::Display for GenericParams {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self.fmt_with_ctx_single_line(&FmtCtx::new()))
}
}

impl std::fmt::Debug for GenericArgs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self)
}
}

impl std::fmt::Debug for GenericParams {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self)
}
}

impl std::fmt::Display for Literal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
Expand All @@ -1633,35 +1645,12 @@ impl std::fmt::Display for Literal {
}
}

impl std::fmt::Display for LiteralTy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
LiteralTy::Integer(ty) => ty.fmt(f),
LiteralTy::Float(ty) => ty.fmt(f),
LiteralTy::Char => write!(f, "char"),
LiteralTy::Bool => write!(f, "bool"),
}
}
}

impl std::fmt::Display for Loc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}:{}", self.line, self.col)
}
}

impl std::fmt::Display for Operand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self.fmt_with_ctx(&FmtCtx::new()))
}
}

impl std::fmt::Display for Place {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self.fmt_with_ctx(&FmtCtx::new()))
}
}

impl std::fmt::Display for RawAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self.path)?;
Expand All @@ -1672,12 +1661,6 @@ impl std::fmt::Display for RawAttribute {
}
}

impl std::fmt::Display for Rvalue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", self.fmt_with_ctx(&FmtCtx::new()))
}
}

impl std::fmt::Display for ScalarValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
Expand Down Expand Up @@ -1715,21 +1698,9 @@ impl std::fmt::Display for TraitItemName {
}
}

impl std::string::ToString for ConstGenericVar {
fn to_string(&self) -> String {
format!("const {} : {}", self.name, self.ty.to_string())
}
}

impl std::string::ToString for Field {
fn to_string(&self) -> String {
self.fmt_with_ctx(&FmtCtx::new())
}
}

impl std::string::ToString for TypeVar {
fn to_string(&self) -> String {
self.name.to_string()
impl std::fmt::Display for TypeVar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
}
}

Expand All @@ -1745,11 +1716,34 @@ impl std::string::ToString for Var {
}
}

impl std::string::ToString for Variant {
fn to_string(&self) -> String {
self.fmt_with_ctx(&FmtCtx::new())
}
macro_rules! impl_display_via_ctx {
($ty:ty) => {
impl Display for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.fmt_with_ctx(&FmtCtx::new()))
}
}
};
}
macro_rules! impl_debug_via_display {
($ty:ty) => {
impl Debug for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<_ as Display>::fmt(self, f)
}
}
};
}

impl_display_via_ctx!(Field);
impl_display_via_ctx!(GenericArgs);
impl_display_via_ctx!(LiteralTy);
impl_display_via_ctx!(Operand);
impl_display_via_ctx!(Place);
impl_display_via_ctx!(Rvalue);
impl_display_via_ctx!(Variant);
impl_debug_via_display!(GenericArgs);
impl_debug_via_display!(GenericParams);

/// Format a function call.
/// We return the pair: (function call, comment)
Expand Down
2 changes: 1 addition & 1 deletion charon/src/pretty/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<'a> Formatter<ConstGenericDbVar> for FmtCtx<'a> {
.and_then(|generics| generics.const_generics.get(varid))
{
None => format!("missing_cg_var({var})"),
Some(v) => v.to_string(),
Some(v) => v.fmt_with_ctx(self),
}
}
}
Expand Down
Loading