From 46923753890f04d12e1d9d230c6eb3b8af28bf40 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 25 May 2023 01:41:13 +0000 Subject: [PATCH] Don't print newlines in APITs --- compiler/rustc_ast_lowering/src/lib.rs | 11 +++++++++- .../arg-position-impl-trait-too-long.rs | 22 +++++++++++++++++++ .../arg-position-impl-trait-too-long.stderr | 22 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/ui/impl-trait/arg-position-impl-trait-too-long.rs create mode 100644 tests/ui/impl-trait/arg-position-impl-trait-too-long.stderr diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 211f5cb0a2a..8d4f96639ef 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1425,7 +1425,16 @@ fn lower_ty_direct(&mut self, t: &Ty, itctx: &ImplTraitContext) -> hir::Ty<'hir> DefPathData::ImplTrait, span, ); - let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span); + + // HACK: pprust breaks strings with newlines when the type + // gets too long. We don't want these to show up in compiler + // output or built artifacts, so replace them here... + // Perhaps we should instead format APITs more robustly. + let ident = Ident::from_str_and_span( + &pprust::ty_to_string(t).replace('\n', " "), + span, + ); + let (param, bounds, path) = self.lower_universal_param_and_bounds( *def_node_id, span, diff --git a/tests/ui/impl-trait/arg-position-impl-trait-too-long.rs b/tests/ui/impl-trait/arg-position-impl-trait-too-long.rs new file mode 100644 index 00000000000..8ef9281c9d3 --- /dev/null +++ b/tests/ui/impl-trait/arg-position-impl-trait-too-long.rs @@ -0,0 +1,22 @@ +struct Header; +struct EntryMetadata; +struct Entry(A, B); + +trait Tr { + type EncodedKey; + type EncodedValue; +} + +fn test( + // This APIT is long, however we shouldn't render the type name with a newline in it. + y: impl FnOnce( + &mut Header, + &mut [EntryMetadata], + &mut [Entry] + ) -> R, +) { + let () = y; + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/impl-trait/arg-position-impl-trait-too-long.stderr b/tests/ui/impl-trait/arg-position-impl-trait-too-long.stderr new file mode 100644 index 00000000000..40446a3d339 --- /dev/null +++ b/tests/ui/impl-trait/arg-position-impl-trait-too-long.stderr @@ -0,0 +1,22 @@ +error[E0308]: mismatched types + --> $DIR/arg-position-impl-trait-too-long.rs:18:9 + | +LL | y: impl FnOnce( + | ________- +LL | | &mut Header, +LL | | &mut [EntryMetadata], +LL | | &mut [Entry] +LL | | ) -> R, + | |__________- this type parameter +LL | ) { +LL | let () = y; + | ^^ - this expression has type `impl FnOnce(&mut Header, &mut [EntryMetadata], &mut [Entry]) -> R` + | | + | expected type parameter `impl FnOnce(&mut Header, &mut [EntryMetadata], &mut [Entry]) -> R`, found `()` + | + = note: expected type parameter `impl FnOnce(&mut Header, &mut [EntryMetadata], &mut [Entry]) -> R` + found unit type `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.