Skip to content

Commit f0a0e24

Browse files
authored
Merge pull request #1669 from dtolnay/pr1658
Changes to PR 1658
2 parents 1cbc3e3 + 69161cb commit f0a0e24

File tree

12 files changed

+303
-344
lines changed

12 files changed

+303
-344
lines changed

gen/src/out.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ impl<'a> OutFile<'a> {
6868
self.content.get_mut().set_namespace(namespace);
6969
}
7070

71-
pub(crate) fn write_fmt(&self, args: Arguments) {
72-
let content = &mut *self.content.borrow_mut();
73-
Write::write_fmt(content, args).unwrap();
74-
}
75-
7671
pub(crate) fn content(&mut self) -> Vec<u8> {
7772
self.flush();
7873

@@ -122,13 +117,6 @@ impl<'a> Write for Content<'a> {
122117
}
123118
}
124119

125-
impl<'a> Write for OutFile<'a> {
126-
fn write_str(&mut self, s: &str) -> fmt::Result {
127-
self.content.borrow_mut().write(s);
128-
Ok(())
129-
}
130-
}
131-
132120
impl<'a> PartialEq for Content<'a> {
133121
fn eq(&self, _other: &Self) -> bool {
134122
true
@@ -241,3 +229,25 @@ impl<'a> BlockBoundary<'a> {
241229
}
242230
}
243231
}
232+
233+
pub(crate) trait InfallibleWrite {
234+
fn write_fmt(&mut self, args: Arguments);
235+
}
236+
237+
impl InfallibleWrite for String {
238+
fn write_fmt(&mut self, args: Arguments) {
239+
Write::write_fmt(self, args).unwrap();
240+
}
241+
}
242+
243+
impl<'a> InfallibleWrite for Content<'a> {
244+
fn write_fmt(&mut self, args: Arguments) {
245+
Write::write_fmt(self, args).unwrap();
246+
}
247+
}
248+
249+
impl<'a> InfallibleWrite for OutFile<'a> {
250+
fn write_fmt(&mut self, args: Arguments) {
251+
InfallibleWrite::write_fmt(self.content.get_mut(), args);
252+
}
253+
}

gen/src/write.rs

Lines changed: 55 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::gen::block::Block;
22
use crate::gen::guard::Guard;
33
use crate::gen::nested::NamespaceEntries;
4-
use crate::gen::out::OutFile;
4+
use crate::gen::out::{InfallibleWrite, OutFile};
55
use crate::gen::{builtin, include, pragma, Opt};
66
use crate::syntax::atom::Atom::{self, *};
77
use crate::syntax::discriminant::{Discriminant, Limits};
@@ -1408,110 +1408,101 @@ fn write_extern_arg(out: &mut OutFile, arg: &Var) {
14081408
}
14091409

14101410
fn write_type(out: &mut OutFile, ty: &Type) {
1411-
write!(out, "{}", stringify_type(ty, out.types));
1411+
write_type_to_generic_writer(out, ty, out.types);
14121412
}
14131413

14141414
fn stringify_type(ty: &Type, types: &Types) -> String {
14151415
let mut s = String::new();
1416-
write_type_to_generic_writer(&mut s, ty, types).unwrap();
1416+
write_type_to_generic_writer(&mut s, ty, types);
14171417
s
14181418
}
14191419

1420-
fn write_type_to_generic_writer(
1421-
out: &mut impl std::fmt::Write,
1422-
ty: &Type,
1423-
types: &Types,
1424-
) -> std::fmt::Result {
1420+
fn write_type_to_generic_writer(out: &mut impl InfallibleWrite, ty: &Type, types: &Types) {
14251421
match ty {
14261422
Type::Ident(ident) => match Atom::from(&ident.rust) {
1427-
Some(atom) => write_atom_to_generic_writer(out, atom),
1423+
Some(atom) => write_atom(out, atom),
14281424
None => write!(out, "{}", types.resolve(ident).name.to_fully_qualified()),
14291425
},
14301426
Type::RustBox(ty) => {
1431-
write!(out, "::rust::Box<")?;
1432-
write_type_to_generic_writer(out, &ty.inner, types)?;
1433-
write!(out, ">")
1427+
write!(out, "::rust::Box<");
1428+
write_type_to_generic_writer(out, &ty.inner, types);
1429+
write!(out, ">");
14341430
}
14351431
Type::RustVec(ty) => {
1436-
write!(out, "::rust::Vec<")?;
1437-
write_type_to_generic_writer(out, &ty.inner, types)?;
1438-
write!(out, ">")
1432+
write!(out, "::rust::Vec<");
1433+
write_type_to_generic_writer(out, &ty.inner, types);
1434+
write!(out, ">");
14391435
}
14401436
Type::UniquePtr(ptr) => {
1441-
write!(out, "::std::unique_ptr<")?;
1442-
write_type_to_generic_writer(out, &ptr.inner, types)?;
1443-
write!(out, ">")
1437+
write!(out, "::std::unique_ptr<");
1438+
write_type_to_generic_writer(out, &ptr.inner, types);
1439+
write!(out, ">");
14441440
}
14451441
Type::SharedPtr(ptr) => {
1446-
write!(out, "::std::shared_ptr<")?;
1447-
write_type_to_generic_writer(out, &ptr.inner, types)?;
1448-
write!(out, ">")
1442+
write!(out, "::std::shared_ptr<");
1443+
write_type_to_generic_writer(out, &ptr.inner, types);
1444+
write!(out, ">");
14491445
}
14501446
Type::WeakPtr(ptr) => {
1451-
write!(out, "::std::weak_ptr<")?;
1452-
write_type_to_generic_writer(out, &ptr.inner, types)?;
1453-
write!(out, ">")
1447+
write!(out, "::std::weak_ptr<");
1448+
write_type_to_generic_writer(out, &ptr.inner, types);
1449+
write!(out, ">");
14541450
}
14551451
Type::CxxVector(ty) => {
1456-
write!(out, "::std::vector<")?;
1457-
write_type_to_generic_writer(out, &ty.inner, types)?;
1458-
write!(out, ">")
1452+
write!(out, "::std::vector<");
1453+
write_type_to_generic_writer(out, &ty.inner, types);
1454+
write!(out, ">");
14591455
}
14601456
Type::Ref(r) => {
1461-
write_type_space_to_generic_writer(out, &r.inner, types)?;
1457+
write_type_space_to_generic_writer(out, &r.inner, types);
14621458
if !r.mutable {
1463-
write!(out, "const ")?;
1459+
write!(out, "const ");
14641460
}
1465-
write!(out, "&")
1461+
write!(out, "&");
14661462
}
14671463
Type::Ptr(p) => {
1468-
write_type_space_to_generic_writer(out, &p.inner, types)?;
1464+
write_type_space_to_generic_writer(out, &p.inner, types);
14691465
if !p.mutable {
1470-
write!(out, "const ")?;
1466+
write!(out, "const ");
14711467
}
1472-
write!(out, "*")
1468+
write!(out, "*");
14731469
}
14741470
Type::Str(_) => {
1475-
write!(out, "::rust::Str")
1471+
write!(out, "::rust::Str");
14761472
}
14771473
Type::SliceRef(slice) => {
1478-
write!(out, "::rust::Slice<")?;
1479-
write_type_space_to_generic_writer(out, &slice.inner, types)?;
1474+
write!(out, "::rust::Slice<");
1475+
write_type_space_to_generic_writer(out, &slice.inner, types);
14801476
if slice.mutability.is_none() {
1481-
write!(out, "const")?;
1477+
write!(out, "const");
14821478
}
1483-
write!(out, ">")
1479+
write!(out, ">");
14841480
}
14851481
Type::Fn(f) => {
1486-
write!(out, "::rust::Fn<")?;
1482+
write!(out, "::rust::Fn<");
14871483
match &f.ret {
1488-
Some(ret) => write_type_to_generic_writer(out, ret, types)?,
1489-
None => write!(out, "void")?,
1484+
Some(ret) => write_type_to_generic_writer(out, ret, types),
1485+
None => write!(out, "void"),
14901486
}
1491-
write!(out, "(")?;
1487+
write!(out, "(");
14921488
for (i, arg) in f.args.iter().enumerate() {
14931489
if i > 0 {
1494-
write!(out, ", ")?;
1490+
write!(out, ", ");
14951491
}
1496-
write_type_to_generic_writer(out, &arg.ty, types)?;
1492+
write_type_to_generic_writer(out, &arg.ty, types);
14971493
}
1498-
write!(out, ")>")
1494+
write!(out, ")>");
14991495
}
15001496
Type::Array(a) => {
1501-
write!(out, "::std::array<")?;
1502-
write_type_to_generic_writer(out, &a.inner, types)?;
1503-
write!(out, ", {}>", &a.len)
1497+
write!(out, "::std::array<");
1498+
write_type_to_generic_writer(out, &a.inner, types);
1499+
write!(out, ", {}>", &a.len);
15041500
}
15051501
Type::Void(_) => unreachable!(),
15061502
}
15071503
}
15081504

1509-
fn write_atom(out: &mut OutFile, atom: Atom) {
1510-
// `unwrap`, because `OutFile`'s impl of `fmt::Write` is infallible.
1511-
write_atom_to_generic_writer(out, atom).unwrap();
1512-
}
1513-
1514-
fn write_atom_to_generic_writer(out: &mut impl std::fmt::Write, atom: Atom) -> std::fmt::Result {
1505+
fn write_atom(out: &mut impl InfallibleWrite, atom: Atom) {
15151506
match atom {
15161507
Bool => write!(out, "bool"),
15171508
Char => write!(out, "char"),
@@ -1533,28 +1524,15 @@ fn write_atom_to_generic_writer(out: &mut impl std::fmt::Write, atom: Atom) -> s
15331524
}
15341525

15351526
fn write_type_space(out: &mut OutFile, ty: &Type) {
1536-
write_type(out, ty);
1537-
write_space_after_type(out, ty);
1538-
}
1539-
1540-
fn write_type_space_to_generic_writer(
1541-
out: &mut impl std::fmt::Write,
1542-
ty: &Type,
1543-
types: &Types,
1544-
) -> std::fmt::Result {
1545-
write_type_to_generic_writer(out, ty, types)?;
1546-
write_space_after_type_to_generic_writer(out, ty)
1527+
write_type_space_to_generic_writer(out, ty, out.types);
15471528
}
15481529

1549-
fn write_space_after_type(out: &mut OutFile, ty: &Type) {
1550-
// `unwrap`, because `OutFile`'s impl of `fmt::Write` is infallible.
1551-
write_space_after_type_to_generic_writer(out, ty).unwrap();
1530+
fn write_type_space_to_generic_writer(out: &mut impl InfallibleWrite, ty: &Type, types: &Types) {
1531+
write_type_to_generic_writer(out, ty, types);
1532+
write_space_after_type(out, ty);
15521533
}
15531534

1554-
fn write_space_after_type_to_generic_writer(
1555-
out: &mut impl std::fmt::Write,
1556-
ty: &Type,
1557-
) -> std::fmt::Result {
1535+
fn write_space_after_type(out: &mut impl InfallibleWrite, ty: &Type) {
15581536
match ty {
15591537
Type::Ident(_)
15601538
| Type::RustBox(_)
@@ -1567,7 +1545,7 @@ fn write_space_after_type_to_generic_writer(
15671545
| Type::SliceRef(_)
15681546
| Type::Fn(_)
15691547
| Type::Array(_) => write!(out, " "),
1570-
Type::Ref(_) | Type::Ptr(_) => Ok(()),
1548+
Type::Ref(_) | Type::Ptr(_) => {}
15711549
Type::Void(_) => unreachable!(),
15721550
}
15731551
}
@@ -1811,13 +1789,13 @@ fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
18111789
out.pragma.missing_declarations = true;
18121790

18131791
let inner = stringify_type(ty, out.types);
1814-
let instance = crate::syntax::mangle::type_(ty)
1815-
.expect("Earlier syntax/check.rs checks should filter out non-mangle-able types");
1792+
let instance = mangle::typename(ty, &out.types.resolutions)
1793+
.expect("unexpected UniquePtr generic parameter allowed through by syntax/check.rs");
18161794

18171795
// Some aliases are to opaque types; some are to trivial types. We can't
18181796
// know at code generation time, so we generate both C++ and Rust side
1819-
// bindings for a "new" method anyway. But the Rust code can't be called
1820-
// for Opaque types because the 'new' method is not implemented.
1797+
// bindings for a "new" method anyway. But the Rust code can't be called for
1798+
// Opaque types because the 'new' method is not implemented.
18211799
let can_construct_from_value = out.types.is_maybe_trivial(ty);
18221800

18231801
out.builtin.is_complete = true;

0 commit comments

Comments
 (0)