Skip to content
Open
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
31 changes: 27 additions & 4 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,30 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
f(value.as_ref().skip_binder(), self)
}

/// Prints comma-separated elements.
/// Prints comma-separated elements. If `comma_sep_has_space` is `true`, there is a space after
/// each comma.
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
where
T: Print<Self>,
{
if let Some(first) = elems.next() {
first.print(self)?;
for elem in elems {
self.write_str(", ")?;
if self.comma_sep_has_space() {
self.write_str(", ")?;
} else {
self.write_str(",")?;
}
elem.print(self)?;
}
}
Ok(())
}

fn comma_sep_has_space(&self) -> bool {
true
}

/// Prints `{f: t}` or `{f as t}` depending on the `cast` argument
fn typed_value(
&mut self,
Expand Down Expand Up @@ -1504,14 +1513,26 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
let splatted_arg_index = splatted.map(usize::from);
let mut input_iter = inputs.iter().copied();
if let Some(index) = splatted_arg_index {
self.comma_sep((&mut input_iter).take(usize::from(index)))?;
write!(self, ", #[rustc_splat]")?;
self.comma_sep((&mut input_iter).take(index))?;
// Splitting the comma-separated list can miss a comma, but we only need that comma if
// there are arguments before the splat.
// FIXME(splat): if splatting becomes part of the type, we can remove this hack
if index > 0 {
if self.comma_sep_has_space() {
write!(self, ", ")?;
} else {
write!(self, ",")?;
}
}
write!(self, "#[rustc_splat] ")?;
self.comma_sep(input_iter)?;
} else {
self.comma_sep(input_iter)?;
}
if c_variadic {
if !inputs.is_empty() {
// In legacy mangling, most comma-separated lists have no spaces. But here we print
// a space before the `...` for readability.
write!(self, ", ")?;
}
write!(self, "...")?;
Expand Down Expand Up @@ -1967,6 +1988,8 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
let mut first = true;
for (field_def, field) in iter::zip(&variant_def.fields, fields) {
if !first {
// FIXME(legacy mangling): if this appears, should we enable
// spaces depending on `comma_sep_has_space`?
write!(self, ", ")?;
}
write!(self, "{}: ", field_def.name)?;
Expand Down
16 changes: 3 additions & 13 deletions compiler/rustc_symbol_mangling/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,19 +487,9 @@ impl<'tcx> PrettyPrinter<'tcx> for LegacySymbolMangler<'tcx> {
false
}

// Identical to `PrettyPrinter::comma_sep` except there is no space after each comma.
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
where
T: Print<Self>,
{
if let Some(first) = elems.next() {
first.print(self)?;
for elem in elems {
self.write_str(",")?;
elem.print(self)?;
}
}
Ok(())
// In symbol mangling, there is no space after the commas in a comma-separated list.
fn comma_sep_has_space(&self) -> bool {
false
}

fn generic_delimiters(
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/splat/splat-fn-ptr-cast-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ LL | let _fn_ptr: fn((u32, i8), f64) = splat_non_terminal_arg;
= note: expected fn pointer `fn((_, _), _)`
found fn item `fn(#[rustc_splat] (_, _), _) {splat_non_terminal_arg}`

error[E0605]: non-primitive cast: `fn(, #[rustc_splat](u32, i8)) {tuple_args}` as `fn((u32, i8))`
error[E0605]: non-primitive cast: `fn(#[rustc_splat] (u32, i8)) {tuple_args}` as `fn((u32, i8))`
--> $DIR/splat-fn-ptr-cast-fail.rs:19:34
|
LL | let _fn_ptr: fn((u32, i8)) = tuple_args as fn((u32, i8));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast

error[E0605]: non-primitive cast: `fn(, #[rustc_splat](u32, i8), f64) {splat_non_terminal_arg}` as `fn((u32, i8), f64)`
error[E0605]: non-primitive cast: `fn(#[rustc_splat] (u32, i8), f64) {splat_non_terminal_arg}` as `fn((u32, i8), f64)`
--> $DIR/splat-fn-ptr-cast-fail.rs:20:39
|
LL | let _fn_ptr: fn((u32, i8), f64) = splat_non_terminal_arg as fn((u32, i8), f64);
Expand Down
Loading
Loading