Skip to content

Make Rust type rules standalone functions#164

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
joaotgouveia:rust-type-rules
Jun 3, 2026
Merged

Make Rust type rules standalone functions#164
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
joaotgouveia:rust-type-rules

Conversation

@joaotgouveia
Copy link
Copy Markdown
Contributor

Changes the format of the Rust side of type rules to:

fn t<digit>() -> TYPE {
    return DEFAULT-INITIALIZER;
}

Actual changes to rules:

  • The initializer for the unsafe rules t1, t2, and t3 in fstream was changed from ::std::fs::File::open("")? to ::std::fs::File::open("").unwrap(). This change was made because the ? operator requires the function to return a value of type Result or Option. Additionally, other rules for file-related types already use ::std::fs::File::open("").unwrap() as an initializer (for example, rule t1 in iostream).

  • Added a missing generic parameter to the refcount rules f10 and f11 in unique_ptr. These rules were relying on the struct defined at the top of the type.

Copy link
Copy Markdown
Contributor

@lucic71 lucic71 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Comment thread rule-preprocessor/src/syntactic.rs Outdated
.find_map(ast::ReturnExpr::cast)
.and_then(|ret| ret.expr())
.or_else(|| body.stmt_list().and_then(|sl| sl.tail_expr()))
.expect("type rule must yield an initializer");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add an assert or change init to only accept { X }, not { return X; } or other type of expressions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't these two alternatives equivalent in practice? I opted to accept both options because I thought the added flexibility might be useful in the future, particularly when generating these rules automatically. Even if that doesn't turn out to be the case, what would be the disadvantages of allowing both forms?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if that doesn't turn out to be the case, what would be the disadvantages of allowing both forms?

None, { X } just feels more natural

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I change it to only allow that form then?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please. And also make sure that we only support:

fn t<digit>() -> TYPE {
    DEFAULT-INITIALIZER
}

not

fn t<digit>() -> TYPE {
    expr1;
    expr2;
    ...
    DEFAULT-INITIALIZER
}

@nunoplopes nunoplopes merged commit 6f8ecae into Cpp2Rust:master Jun 3, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants