From 7f5a19ba209561394c35e7a2fd0257299093d491 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 6 Oct 2022 21:39:37 +0000 Subject: [PATCH 01/14] DiagnosticBuilder -> Diagnostic --- .../src/diagnostics/mutability_errors.rs | 19 ++++++------------- compiler/rustc_passes/src/liveness.rs | 3 ++- .../rustc_resolve/src/late/diagnostics.rs | 10 +++++----- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 8ad40c0aa0a5d..1cb2e5e326014 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1,6 +1,4 @@ -use rustc_errors::{ - Applicability, Diagnostic, DiagnosticBuilder, EmissionGuarantee, ErrorGuaranteed, -}; +use rustc_errors::{Applicability, Diagnostic}; use rustc_hir as hir; use rustc_hir::intravisit::Visitor; use rustc_hir::Node; @@ -629,25 +627,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { self.buffer_error(err); } - fn suggest_map_index_mut_alternatives( - &self, - ty: Ty<'_>, - err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>, - span: Span, - ) { + fn suggest_map_index_mut_alternatives(&self, ty: Ty<'tcx>, err: &mut Diagnostic, span: Span) { let Some(adt) = ty.ty_adt_def() else { return }; let did = adt.did(); if self.infcx.tcx.is_diagnostic_item(sym::HashMap, did) || self.infcx.tcx.is_diagnostic_item(sym::BTreeMap, did) { - struct V<'a, 'b, 'tcx, G: EmissionGuarantee> { + struct V<'a, 'tcx> { assign_span: Span, - err: &'a mut DiagnosticBuilder<'b, G>, + err: &'a mut Diagnostic, ty: Ty<'tcx>, suggested: bool, } - impl<'a, 'b: 'a, 'hir, 'tcx, G: EmissionGuarantee> Visitor<'hir> for V<'a, 'b, 'tcx, G> { - fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'hir>) { + impl<'a, 'tcx> Visitor<'tcx> for V<'a, 'tcx> { + fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) { hir::intravisit::walk_stmt(self, stmt); let expr = match stmt.kind { hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr) => expr, diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index c6fe40f72fc63..c181de48a9ad8 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -87,6 +87,7 @@ use self::VarKind::*; use rustc_ast::InlineAsmOptions; use rustc_data_structures::fx::FxIndexMap; use rustc_errors::Applicability; +use rustc_errors::Diagnostic; use rustc_hir as hir; use rustc_hir::def::*; use rustc_hir::def_id::{DefId, LocalDefId}; @@ -1690,7 +1691,7 @@ impl<'tcx> Liveness<'_, 'tcx> { &self, name: &str, opt_body: Option<&hir::Body<'_>>, - err: &mut rustc_errors::DiagnosticBuilder<'_, ()>, + err: &mut Diagnostic, ) -> bool { let mut has_litstring = false; let Some(opt_body) = opt_body else {return false;}; diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 2d339a4d07041..0fce56e3ddc11 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -405,7 +405,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { fn try_lookup_name_relaxed( &mut self, - err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>, + err: &mut Diagnostic, source: PathSource<'_>, path: &[Segment], span: Span, @@ -465,7 +465,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { .contains(span) { // Already reported this issue on the lhs of the type ascription. - err.delay_as_bug(); + err.downgrade_to_delayed_bug(); return (true, candidates); } } @@ -576,7 +576,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { fn suggest_trait_and_bounds( &mut self, - err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>, + err: &mut Diagnostic, source: PathSource<'_>, res: Option, span: Span, @@ -651,7 +651,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { fn suggest_typo( &mut self, - err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>, + err: &mut Diagnostic, source: PathSource<'_>, path: &[Segment], span: Span, @@ -686,7 +686,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { fn err_code_special_cases( &mut self, - err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>, + err: &mut Diagnostic, source: PathSource<'_>, path: &[Segment], span: Span, From c1538f01cd073f257af97532340d49913d94835d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 7 Oct 2022 02:05:57 +0000 Subject: [PATCH 02/14] Make span_suggestions take IntoIterator --- .../rustc_borrowck/src/diagnostics/mutability_errors.rs | 2 +- compiler/rustc_errors/src/diagnostic.rs | 7 ++++--- compiler/rustc_errors/src/diagnostic_builder.rs | 4 ++-- compiler/rustc_parse/src/parser/ty.rs | 2 +- compiler/rustc_resolve/src/late/diagnostics.rs | 6 +++--- .../src/traits/error_reporting/suggestions.rs | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 1cb2e5e326014..7457369aa58cb 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -698,7 +698,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { ), (rv.span.shrink_to_hi(), ")".to_string()), ], - ].into_iter(), + ], Applicability::MachineApplicable, ); self.suggested = true; diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 31e410aaaf082..35c582abbde79 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -811,10 +811,10 @@ impl Diagnostic { &mut self, sp: Span, msg: impl Into, - suggestions: impl Iterator, + suggestions: impl IntoIterator, applicability: Applicability, ) -> &mut Self { - let mut suggestions: Vec<_> = suggestions.collect(); + let mut suggestions: Vec<_> = suggestions.into_iter().collect(); suggestions.sort(); let substitutions = suggestions .into_iter() @@ -834,11 +834,12 @@ impl Diagnostic { pub fn multipart_suggestions( &mut self, msg: impl Into, - suggestions: impl Iterator>, + suggestions: impl IntoIterator>, applicability: Applicability, ) -> &mut Self { self.push_suggestion(CodeSuggestion { substitutions: suggestions + .into_iter() .map(|sugg| Substitution { parts: sugg .into_iter() diff --git a/compiler/rustc_errors/src/diagnostic_builder.rs b/compiler/rustc_errors/src/diagnostic_builder.rs index bbe6435be59f4..3d3009d7475c0 100644 --- a/compiler/rustc_errors/src/diagnostic_builder.rs +++ b/compiler/rustc_errors/src/diagnostic_builder.rs @@ -544,13 +544,13 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> { &mut self, sp: Span, msg: impl Into, - suggestions: impl Iterator, + suggestions: impl IntoIterator, applicability: Applicability, ) -> &mut Self); forward!(pub fn multipart_suggestions( &mut self, msg: impl Into, - suggestions: impl Iterator>, + suggestions: impl IntoIterator>, applicability: Applicability, ) -> &mut Self); forward!(pub fn span_suggestion_short( diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 2a8512acf8cfa..3a67c032b3bdd 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -401,7 +401,7 @@ impl<'a> Parser<'a> { .span_suggestions( span.shrink_to_hi(), "add `mut` or `const` here", - ["mut ".to_string(), "const ".to_string()].into_iter(), + ["mut ".to_string(), "const ".to_string()], Applicability::HasPlaceholders, ) .emit(); diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 0fce56e3ddc11..b08e66175cf60 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1877,7 +1877,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { err.span_suggestions( span, &msg, - suggestable_variants.into_iter(), + suggestable_variants, Applicability::MaybeIncorrect, ); } @@ -1931,7 +1931,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { err.span_suggestions( span, msg, - suggestable_variants.into_iter(), + suggestable_variants, Applicability::MaybeIncorrect, ); } @@ -1961,7 +1961,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { err.span_suggestions( span, msg, - suggestable_variants_with_placeholders.into_iter(), + suggestable_variants_with_placeholders, Applicability::HasPlaceholders, ); } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 7aae014af6044..9c4afc800bf22 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1025,7 +1025,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { err.span_suggestions( span.shrink_to_lo(), "consider borrowing here", - ["&".to_string(), "&mut ".to_string()].into_iter(), + ["&".to_string(), "&mut ".to_string()], Applicability::MaybeIncorrect, ); } else { From 419fde7a38fab7c0355d4091ddafd8d775e63f3f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 21 Oct 2022 14:56:53 +0000 Subject: [PATCH 03/14] Handle RPITITs properly in register_hidden_type --- compiler/rustc_infer/src/infer/opaque_types.rs | 14 +++++++++++++- .../impl-trait/in-trait/default-body-type-err-2.rs | 13 +++++++++++++ .../in-trait/default-body-type-err-2.stderr | 11 +++++++++++ .../impl-trait/in-trait/default-body-type-err.rs | 13 +++++++++++++ .../in-trait/default-body-type-err.stderr | 12 ++++++++++++ .../impl-trait/in-trait/default-body-with-rpit.rs | 2 +- .../in-trait/default-body-with-rpit.stderr | 12 ------------ 7 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 src/test/ui/impl-trait/in-trait/default-body-type-err-2.rs create mode 100644 src/test/ui/impl-trait/in-trait/default-body-type-err-2.stderr create mode 100644 src/test/ui/impl-trait/in-trait/default-body-type-err.rs create mode 100644 src/test/ui/impl-trait/in-trait/default-body-type-err.stderr delete mode 100644 src/test/ui/impl-trait/in-trait/default-body-with-rpit.stderr diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types.rs index 77e8f72aefac0..cc8b77d2cfeac 100644 --- a/compiler/rustc_infer/src/infer/opaque_types.rs +++ b/compiler/rustc_infer/src/infer/opaque_types.rs @@ -1,6 +1,7 @@ use crate::errors::OpaqueHiddenTypeDiag; use crate::infer::{DefiningAnchor, InferCtxt, InferOk}; use crate::traits; +use hir::def::DefKind; use hir::def_id::{DefId, LocalDefId}; use hir::{HirId, OpaqueTyOrigin}; use rustc_data_structures::sync::Lrc; @@ -552,7 +553,12 @@ impl<'tcx> InferCtxt<'tcx> { ty_op: |ty| match *ty.kind() { // We can't normalize associated types from `rustc_infer`, // but we can eagerly register inference variables for them. - ty::Projection(projection_ty) if !projection_ty.has_escaping_bound_vars() => { + // FIXME(RPITIT): Don't replace RPITITs with inference vars. + ty::Projection(projection_ty) + if !projection_ty.has_escaping_bound_vars() + && tcx.def_kind(projection_ty.item_def_id) + != DefKind::ImplTraitPlaceholder => + { self.infer_projection( param_env, projection_ty, @@ -568,6 +574,12 @@ impl<'tcx> InferCtxt<'tcx> { { hidden_ty } + // FIXME(RPITIT): This can go away when we move to associated types + ty::Projection(proj) + if def_id.to_def_id() == proj.item_def_id && substs == proj.substs => + { + hidden_ty + } _ => ty, }, lt_op: |lt| lt, diff --git a/src/test/ui/impl-trait/in-trait/default-body-type-err-2.rs b/src/test/ui/impl-trait/in-trait/default-body-type-err-2.rs new file mode 100644 index 0000000000000..45ae2b8ad3a69 --- /dev/null +++ b/src/test/ui/impl-trait/in-trait/default-body-type-err-2.rs @@ -0,0 +1,13 @@ +// edition:2021 + +#![allow(incomplete_features)] +#![feature(async_fn_in_trait)] + +pub trait Foo { + async fn woopsie_async(&self) -> String { + 42 + //~^ ERROR mismatched types + } +} + +fn main() {} diff --git a/src/test/ui/impl-trait/in-trait/default-body-type-err-2.stderr b/src/test/ui/impl-trait/in-trait/default-body-type-err-2.stderr new file mode 100644 index 0000000000000..142b1bff1a46a --- /dev/null +++ b/src/test/ui/impl-trait/in-trait/default-body-type-err-2.stderr @@ -0,0 +1,11 @@ +error[E0308]: mismatched types + --> $DIR/default-body-type-err-2.rs:8:9 + | +LL | 42 + | ^^- help: try using a conversion method: `.to_string()` + | | + | expected struct `String`, found integer + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/impl-trait/in-trait/default-body-type-err.rs b/src/test/ui/impl-trait/in-trait/default-body-type-err.rs new file mode 100644 index 0000000000000..ac9baf91cae37 --- /dev/null +++ b/src/test/ui/impl-trait/in-trait/default-body-type-err.rs @@ -0,0 +1,13 @@ +#![allow(incomplete_features)] +#![feature(return_position_impl_trait_in_trait)] + +use std::ops::Deref; + +pub trait Foo { + fn lol(&self) -> impl Deref { + //~^ type mismatch resolving `<&i32 as Deref>::Target == String` + &1i32 + } +} + +fn main() {} diff --git a/src/test/ui/impl-trait/in-trait/default-body-type-err.stderr b/src/test/ui/impl-trait/in-trait/default-body-type-err.stderr new file mode 100644 index 0000000000000..461247a3e3ff4 --- /dev/null +++ b/src/test/ui/impl-trait/in-trait/default-body-type-err.stderr @@ -0,0 +1,12 @@ +error[E0271]: type mismatch resolving `<&i32 as Deref>::Target == String` + --> $DIR/default-body-type-err.rs:7:22 + | +LL | fn lol(&self) -> impl Deref { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found struct `String` +LL | +LL | &1i32 + | ----- return type was inferred to be `&i32` here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/src/test/ui/impl-trait/in-trait/default-body-with-rpit.rs b/src/test/ui/impl-trait/in-trait/default-body-with-rpit.rs index f0d407cd527d6..ad3cc7c2524b9 100644 --- a/src/test/ui/impl-trait/in-trait/default-body-with-rpit.rs +++ b/src/test/ui/impl-trait/in-trait/default-body-with-rpit.rs @@ -1,4 +1,4 @@ -// known-bug: #102688 +// check-pass // edition:2021 #![feature(async_fn_in_trait, return_position_impl_trait_in_trait)] diff --git a/src/test/ui/impl-trait/in-trait/default-body-with-rpit.stderr b/src/test/ui/impl-trait/in-trait/default-body-with-rpit.stderr deleted file mode 100644 index 4529d301f9e91..0000000000000 --- a/src/test/ui/impl-trait/in-trait/default-body-with-rpit.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0720]: cannot resolve opaque type - --> $DIR/default-body-with-rpit.rs:10:28 - | -LL | async fn baz(&self) -> impl Debug { - | ^^^^^^^^^^ cannot resolve opaque type - | - = note: these returned values have a concrete "never" type - = help: this error will resolve once the item's body returns a concrete type - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0720`. From 9f6b9dd64465251b4167a442077feb6e5dd9fbda Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 21 Oct 2022 18:53:16 +0000 Subject: [PATCH 04/14] Delay ambiguity span bug in normalize query iff not rustdoc --- .../src/traits/query/normalize.rs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index aa8094a60dd08..715f5be8e2f4b 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -14,6 +14,7 @@ use rustc_infer::traits::Normalized; use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable}; use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor}; +use rustc_span::DUMMY_SP; use std::ops::ControlFlow; @@ -253,7 +254,15 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> { let result = tcx.normalize_projection_ty(c_data)?; // We don't expect ambiguity. if result.is_ambiguous() { - bug!("unexpected ambiguity: {:?} {:?}", c_data, result); + // Rustdoc normalizes possibly not well-formed types, so only + // treat this as a bug if we're not in rustdoc. + if !tcx.sess.opts.actually_rustdoc { + tcx.sess.delay_span_bug( + DUMMY_SP, + format!("unexpected ambiguity: {:?} {:?}", c_data, result), + ); + } + return Err(NoSolution); } let InferOk { value: result, obligations } = self.infcx.instantiate_query_response_and_region_obligations( @@ -296,7 +305,15 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> { let result = tcx.normalize_projection_ty(c_data)?; // We don't expect ambiguity. if result.is_ambiguous() { - bug!("unexpected ambiguity: {:?} {:?}", c_data, result); + // Rustdoc normalizes possibly not well-formed types, so only + // treat this as a bug if we're not in rustdoc. + if !tcx.sess.opts.actually_rustdoc { + tcx.sess.delay_span_bug( + DUMMY_SP, + format!("unexpected ambiguity: {:?} {:?}", c_data, result), + ); + } + return Err(NoSolution); } let InferOk { value: result, obligations } = self.infcx.instantiate_query_response_and_region_obligations( From cb61113cdc158de18993a94bc6bb39aee6950a9b Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 21 Oct 2022 18:53:27 +0000 Subject: [PATCH 05/14] tests --- .../rustdoc/not-wf-ambiguous-normalization.rs | 24 ++++++ src/test/ui/impl-trait/issue-103181-1.rs | 85 +++++++++++++++++++ src/test/ui/impl-trait/issue-103181-1.stderr | 12 +++ src/test/ui/impl-trait/issue-103181-2.rs | 29 +++++++ src/test/ui/impl-trait/issue-103181-2.stderr | 9 ++ 5 files changed, 159 insertions(+) create mode 100644 src/test/rustdoc/not-wf-ambiguous-normalization.rs create mode 100644 src/test/ui/impl-trait/issue-103181-1.rs create mode 100644 src/test/ui/impl-trait/issue-103181-1.stderr create mode 100644 src/test/ui/impl-trait/issue-103181-2.rs create mode 100644 src/test/ui/impl-trait/issue-103181-2.stderr diff --git a/src/test/rustdoc/not-wf-ambiguous-normalization.rs b/src/test/rustdoc/not-wf-ambiguous-normalization.rs new file mode 100644 index 0000000000000..1e9f925f8458d --- /dev/null +++ b/src/test/rustdoc/not-wf-ambiguous-normalization.rs @@ -0,0 +1,24 @@ +// compile-flags: -Znormalize-docs + +#![feature(type_alias_impl_trait)] + +trait Allocator { + type Buffer; +} + +struct DefaultAllocator; + +// This unconstrained impl parameter causes the normalization of +// `::Buffer` to be ambiguous, +// which caused an ICE with `-Znormalize-docs`. +impl Allocator for DefaultAllocator { + type Buffer = (); +} + +type A = impl Fn(::Buffer); + +fn foo() -> A { + |_| () +} + +fn main() {} diff --git a/src/test/ui/impl-trait/issue-103181-1.rs b/src/test/ui/impl-trait/issue-103181-1.rs new file mode 100644 index 0000000000000..197aedf9d98bc --- /dev/null +++ b/src/test/ui/impl-trait/issue-103181-1.rs @@ -0,0 +1,85 @@ +// edition:2021 + +mod hyper { + use std::{fmt::Debug, future::Future, marker::PhantomData, pin::Pin, task::Poll}; + + pub trait HttpBody { + type Error; + } + impl HttpBody for () { + //~^ ERROR not all trait items implemented, missing: `Error` + // don't implement `Error` here for the ICE + } + + pub struct Server(I, S); + + pub fn serve(_: S) -> Server { + todo!() + } + + impl Future for Server<(), S> + where + S: MakeServiceRef<(), (), ResBody = B>, + B: HttpBody, + B::Error: Debug, + { + type Output = (); + + fn poll(self: Pin<&mut Self>, _: &mut std::task::Context<'_>) -> Poll { + todo!() + } + } + + pub trait MakeServiceRef { + type ResBody; + } + + impl MakeServiceRef<(), ()> for T + where + T: for<'a> Service<&'a (), Response = S>, + S: Service<()>, + { + type ResBody = (); + } + + pub struct MakeServiceFn(pub F); + pub struct ServiceFn(pub PhantomData<(F, R)>); + + pub trait Service { + type Response; + } + + impl<'t, F, Ret, Target, Svc> Service<&'t Target> for MakeServiceFn + where + F: Fn() -> Ret, + Ret: Future>, + { + type Response = Svc; + } + + impl Service for ServiceFn + where + F: Fn() -> Ret, + Ret: Future>, + { + type Response = ResBody; + } +} + +async fn smarvice() -> Result<(), ()> { + Ok(()) +} + +fn service_fn(f: F) -> hyper::ServiceFn +where + F: Fn() -> S, +{ + hyper::ServiceFn(std::marker::PhantomData) +} + +async fn iceice() { + let service = hyper::MakeServiceFn(|| async { Ok::<_, ()>(service_fn(|| smarvice())) }); + hyper::serve::<(), _>(service).await; +} + +fn main() {} diff --git a/src/test/ui/impl-trait/issue-103181-1.stderr b/src/test/ui/impl-trait/issue-103181-1.stderr new file mode 100644 index 0000000000000..cd026607d52fc --- /dev/null +++ b/src/test/ui/impl-trait/issue-103181-1.stderr @@ -0,0 +1,12 @@ +error[E0046]: not all trait items implemented, missing: `Error` + --> $DIR/issue-103181-1.rs:9:5 + | +LL | type Error; + | ---------- `Error` from trait +LL | } +LL | impl HttpBody for () { + | ^^^^^^^^^^^^^^^^^^^^ missing `Error` in implementation + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0046`. diff --git a/src/test/ui/impl-trait/issue-103181-2.rs b/src/test/ui/impl-trait/issue-103181-2.rs new file mode 100644 index 0000000000000..b43ac45075e2b --- /dev/null +++ b/src/test/ui/impl-trait/issue-103181-2.rs @@ -0,0 +1,29 @@ +// edition:2021 + +trait SendFuture: Send { + type Output; +} + +impl SendFuture for Fut { + type Output = (); +} + +async fn broken_fut() { + ident_error; + //~^ ERROR cannot find value `ident_error` in this scope +} + +// triggers normalization of `::Output`, +// which requires `Fut: Send`. +fn normalize(_: Fut, _: Fut::Output) {} + +async fn iceice() +// <- async fn is necessary +where + A: Send, + B: Send, // <- a second bound +{ + normalize(broken_fut(), ()); +} + +fn main() {} diff --git a/src/test/ui/impl-trait/issue-103181-2.stderr b/src/test/ui/impl-trait/issue-103181-2.stderr new file mode 100644 index 0000000000000..5eb2dd9184bec --- /dev/null +++ b/src/test/ui/impl-trait/issue-103181-2.stderr @@ -0,0 +1,9 @@ +error[E0425]: cannot find value `ident_error` in this scope + --> $DIR/issue-103181-2.rs:12:5 + | +LL | ident_error; + | ^^^^^^^^^^^ not found in this scope + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0425`. From 7280f3d28aa139cec0c75072a3e66294b7f99b59 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 21 Oct 2022 17:44:35 -0700 Subject: [PATCH 06/14] Truncate thread names on Linux and Apple targets These targets have system limits on the thread names, 16 and 64 bytes respectively, and `pthread_setname_np` returns an error if the name is longer. However, we're not in a context that can propagate errors when we call this, and we used to implicitly truncate on Linux with `prctl`, so now we manually truncate these names ahead of time. --- library/std/src/sys/unix/thread.rs | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index 42ac6fcd8bf36..6a533854fad3d 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -132,8 +132,11 @@ impl Thread { #[cfg(target_os = "linux")] pub fn set_name(name: &CStr) { + const TASK_COMM_LEN: usize = 16; + unsafe { // Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20. + let name = truncate_cstr(name, TASK_COMM_LEN); libc::pthread_setname_np(libc::pthread_self(), name.as_ptr()); } } @@ -148,6 +151,7 @@ impl Thread { #[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))] pub fn set_name(name: &CStr) { unsafe { + let name = truncate_cstr(name, libc::MAXTHREADNAMESIZE); libc::pthread_setname_np(name.as_ptr()); } } @@ -276,6 +280,20 @@ impl Drop for Thread { } } +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios", target_os = "watchos"))] +fn truncate_cstr(cstr: &CStr, max_with_nul: usize) -> crate::borrow::Cow<'_, CStr> { + use crate::{borrow::Cow, ffi::CString}; + + if cstr.to_bytes_with_nul().len() > max_with_nul { + let bytes = cstr.to_bytes()[..max_with_nul - 1].to_vec(); + // SAFETY: the non-nul bytes came straight from a CStr. + // (CString will add the terminating nul.) + Cow::Owned(unsafe { CString::from_vec_unchecked(bytes) }) + } else { + Cow::Borrowed(cstr) + } +} + pub fn available_parallelism() -> io::Result { cfg_if::cfg_if! { if #[cfg(any( @@ -902,3 +920,28 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { 2048 // just a guess } + +#[test] +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios", target_os = "watchos"))] +fn test_named_thread_truncation() { + use crate::thread::{self, Builder}; + + let long_name = crate::iter::once("test_named_thread_truncation") + .chain(crate::iter::repeat(" yada").take(100)) + .collect::(); + + let result = Builder::new().name(long_name.clone()).spawn(move || { + // Rust remembers the full thread name itself. + assert_eq!(thread::current().name(), Some(long_name.as_str())); + + // But the kernel is limited -- make sure we successfully set a truncation. + let mut buf = vec![0u8; long_name.len() + 1]; + unsafe { + libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len()); + } + let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); + assert!(cstr.to_bytes().len() > 0); + assert!(long_name.as_bytes().starts_with(cstr.to_bytes())); + }); + result.unwrap().join().unwrap(); +} From 12e45846ebbc32bd6a56f2de2658d3d9ad459032 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 21 Oct 2022 18:13:22 -0700 Subject: [PATCH 07/14] Move truncation next to other thread tests for tidy --- library/std/src/sys/unix/thread.rs | 25 ------------------------- library/std/src/thread/tests.rs | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index 6a533854fad3d..69cd2b500a1f0 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -920,28 +920,3 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { 2048 // just a guess } - -#[test] -#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios", target_os = "watchos"))] -fn test_named_thread_truncation() { - use crate::thread::{self, Builder}; - - let long_name = crate::iter::once("test_named_thread_truncation") - .chain(crate::iter::repeat(" yada").take(100)) - .collect::(); - - let result = Builder::new().name(long_name.clone()).spawn(move || { - // Rust remembers the full thread name itself. - assert_eq!(thread::current().name(), Some(long_name.as_str())); - - // But the kernel is limited -- make sure we successfully set a truncation. - let mut buf = vec![0u8; long_name.len() + 1]; - unsafe { - libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len()); - } - let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); - assert!(cstr.to_bytes().len() > 0); - assert!(long_name.as_bytes().starts_with(cstr.to_bytes())); - }); - result.unwrap().join().unwrap(); -} diff --git a/library/std/src/thread/tests.rs b/library/std/src/thread/tests.rs index dfb8765ab4eed..71eb41cd564db 100644 --- a/library/std/src/thread/tests.rs +++ b/library/std/src/thread/tests.rs @@ -37,6 +37,31 @@ fn test_named_thread() { .unwrap(); } +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios", target_os = "watchos"))] +#[test] +fn test_named_thread_truncation() { + use crate::ffi::CStr; + + let long_name = crate::iter::once("test_named_thread_truncation") + .chain(crate::iter::repeat(" yada").take(100)) + .collect::(); + + let result = Builder::new().name(long_name.clone()).spawn(move || { + // Rust remembers the full thread name itself. + assert_eq!(thread::current().name(), Some(long_name.as_str())); + + // But the system is limited -- make sure we successfully set a truncation. + let mut buf = vec![0u8; long_name.len() + 1]; + unsafe { + libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len()); + } + let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); + assert!(cstr.to_bytes().len() > 0); + assert!(long_name.as_bytes().starts_with(cstr.to_bytes())); + }); + result.unwrap().join().unwrap(); +} + #[test] #[should_panic] fn test_invalid_named_thread() { From ed40d461590ab0101ccec7610b8d8faecc94f7ee Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sat, 22 Oct 2022 02:37:15 +0100 Subject: [PATCH 08/14] Properly escape quotes when suggesting switching between char/string literals --- .../src/infer/error_reporting/mod.rs | 23 +++++++++++++++++-- .../src/lexer/unescape_error_reporting.rs | 19 +++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index ddeeaa9618e60..88ffbe0b539ac 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2272,6 +2272,25 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { struct_span_err!(self.tcx.sess, span, E0580, "{}", failure_str) } FailureCode::Error0308(failure_str) => { + fn escape_literal(s: &str) -> String { + let mut escaped = String::with_capacity(s.len()); + let mut chrs = s.chars().peekable(); + while let Some(first) = chrs.next() { + match (first, chrs.peek()) { + ('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => { + escaped.push('\\'); + escaped.push(delim); + chrs.next(); + } + ('"' | '\'', _) => { + escaped.push('\\'); + escaped.push(first) + } + (c, _) => escaped.push(c), + }; + } + escaped + } let mut err = struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str); if let Some((expected, found)) = trace.values.ty() { match (expected.kind(), found.kind()) { @@ -2293,7 +2312,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { err.span_suggestion( span, "if you meant to write a `char` literal, use single quotes", - format!("'{}'", code), + format!("'{}'", escape_literal(code)), Applicability::MachineApplicable, ); } @@ -2308,7 +2327,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { err.span_suggestion( span, "if you meant to write a `str` literal, use double quotes", - format!("\"{}\"", code), + format!("\"{}\"", escape_literal(code)), Applicability::MachineApplicable, ); } diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index 77c4fadab45ea..f075de7142676 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -113,11 +113,26 @@ pub(crate) fn emit_unescape_error( } else { ("", "if you meant to write a `str` literal, use double quotes") }; - + let mut escaped = String::with_capacity(lit.len()); + let mut chrs = lit.chars().peekable(); + while let Some(first) = chrs.next() { + match (first, chrs.peek()) { + ('\\', Some('"')) => { + escaped.push('\\'); + escaped.push('"'); + chrs.next(); + } + ('"', _) => { + escaped.push('\\'); + escaped.push('"') + } + (c, _) => escaped.push(c), + }; + } handler.span_suggestion( span_with_quotes, msg, - format!("{}\"{}\"", prefix, lit), + format!("{prefix}\"{escaped}\""), Applicability::MachineApplicable, ); } From 876248506c9dcb0f4f57db49021374bb0bfc1bd7 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sat, 22 Oct 2022 02:37:20 +0100 Subject: [PATCH 09/14] Update UI tests --- .../ui/inference/char-as-str-single.fixed | 1 + src/test/ui/inference/char-as-str-single.rs | 1 + .../ui/inference/char-as-str-single.stderr | 15 +++++++++++- src/test/ui/inference/str-as-char.fixed | 4 +++- src/test/ui/inference/str-as-char.rs | 4 +++- src/test/ui/inference/str-as-char.stderr | 24 ++++++++++++++++++- 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/test/ui/inference/char-as-str-single.fixed b/src/test/ui/inference/char-as-str-single.fixed index e401492a830b4..bab1854dc5173 100644 --- a/src/test/ui/inference/char-as-str-single.fixed +++ b/src/test/ui/inference/char-as-str-single.fixed @@ -8,4 +8,5 @@ fn main() { let _: char = 'a'; //~ ERROR mismatched types let _: char = '人'; //~ ERROR mismatched types + let _: char = '\''; //~ ERROR mismatched types } diff --git a/src/test/ui/inference/char-as-str-single.rs b/src/test/ui/inference/char-as-str-single.rs index 4f23cea5354c3..736920643b2c4 100644 --- a/src/test/ui/inference/char-as-str-single.rs +++ b/src/test/ui/inference/char-as-str-single.rs @@ -8,4 +8,5 @@ fn main() { let _: char = "a"; //~ ERROR mismatched types let _: char = "人"; //~ ERROR mismatched types + let _: char = "'"; //~ ERROR mismatched types } diff --git a/src/test/ui/inference/char-as-str-single.stderr b/src/test/ui/inference/char-as-str-single.stderr index 29075c15414b2..3375ec6ac32ca 100644 --- a/src/test/ui/inference/char-as-str-single.stderr +++ b/src/test/ui/inference/char-as-str-single.stderr @@ -24,6 +24,19 @@ help: if you meant to write a `char` literal, use single quotes LL | let _: char = '人'; | ~~~~ -error: aborting due to 2 previous errors +error[E0308]: mismatched types + --> $DIR/char-as-str-single.rs:11:19 + | +LL | let _: char = "'"; + | ---- ^^^ expected `char`, found `&str` + | | + | expected due to this + | +help: if you meant to write a `char` literal, use single quotes + | +LL | let _: char = '\''; + | ~~~~ + +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/inference/str-as-char.fixed b/src/test/ui/inference/str-as-char.fixed index 09f3dec5a1755..6aea809cbdb29 100644 --- a/src/test/ui/inference/str-as-char.fixed +++ b/src/test/ui/inference/str-as-char.fixed @@ -4,5 +4,7 @@ // run-rustfix fn main() { - let _: &str = "a"; //~ ERROR mismatched types + let _: &str = "a"; //~ ERROR mismatched types + let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint + let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint } diff --git a/src/test/ui/inference/str-as-char.rs b/src/test/ui/inference/str-as-char.rs index 7092a61244255..eaa8d788c3467 100644 --- a/src/test/ui/inference/str-as-char.rs +++ b/src/test/ui/inference/str-as-char.rs @@ -4,5 +4,7 @@ // run-rustfix fn main() { - let _: &str = 'a'; //~ ERROR mismatched types + let _: &str = 'a'; //~ ERROR mismatched types + let _: &str = '"""'; //~ ERROR character literal may only contain one codepoint + let _: &str = '\"\"\"'; //~ ERROR character literal may only contain one codepoint } diff --git a/src/test/ui/inference/str-as-char.stderr b/src/test/ui/inference/str-as-char.stderr index ebbe7c80f7719..2c84dac8e0c15 100644 --- a/src/test/ui/inference/str-as-char.stderr +++ b/src/test/ui/inference/str-as-char.stderr @@ -1,3 +1,25 @@ +error: character literal may only contain one codepoint + --> $DIR/str-as-char.rs:8:19 + | +LL | let _: &str = '"""'; + | ^^^^^ + | +help: if you meant to write a `str` literal, use double quotes + | +LL | let _: &str = "\"\"\""; + | ~~~~~~~~ + +error: character literal may only contain one codepoint + --> $DIR/str-as-char.rs:9:19 + | +LL | let _: &str = '\"\"\"'; + | ^^^^^^^^ + | +help: if you meant to write a `str` literal, use double quotes + | +LL | let _: &str = "\"\"\""; + | ~~~~~~~~ + error[E0308]: mismatched types --> $DIR/str-as-char.rs:7:19 | @@ -11,6 +33,6 @@ help: if you meant to write a `str` literal, use double quotes LL | let _: &str = "a"; | ~~~ -error: aborting due to previous error +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0308`. From 2ac29f6808601322612559dc15e7a20fd815be24 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 21 Oct 2022 22:06:16 -0700 Subject: [PATCH 10/14] rustdoc: remove unused CSS class `.result-description` It was added to the CSS in d8de2b4c338471aacaf0e8a096f9a7148b146ab4, but was never actually used in that PR. --- src/librustdoc/html/static/css/rustdoc.css | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 9785e1f54c1a4..324f5ffb4a918 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -920,7 +920,7 @@ so that we can apply CSS-filters to change the arrow color in themes */ flex-flow: row wrap; } -.search-results .result-name, .search-results div.desc, .search-results .result-description { +.search-results .result-name, .search-results div.desc { width: 50%; } .search-results .result-name { @@ -1899,10 +1899,10 @@ in storage.js border-bottom: 1px solid #aaa9; padding: 5px 0px; } - .search-results .result-name, .search-results div.desc, .search-results .result-description { + .search-results .result-name, .search-results div.desc { width: 100%; } - .search-results div.desc, .search-results .result-description, .item-right { + .search-results div.desc, .item-right { padding-left: 2em; } From aa8931c61235d4ff3a87665229614da6ac0a0ee5 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 16 Oct 2022 18:48:28 +0000 Subject: [PATCH 11/14] Introduce subst_iter and subst_iter_copied on EarlyBinder --- .../src/check/compare_method.rs | 16 +++------- compiler/rustc_hir_typeck/src/_match.rs | 7 +++-- compiler/rustc_hir_typeck/src/closure.rs | 31 +++++++++---------- .../src/infer/error_reporting/mod.rs | 3 +- .../rustc_infer/src/infer/opaque_types.rs | 5 +-- .../src/opaque_hidden_inferred_bound.rs | 12 +++---- compiler/rustc_middle/src/ty/print/pretty.rs | 3 +- compiler/rustc_middle/src/ty/subst.rs | 23 ++++++++++++++ src/tools/clippy/clippy_utils/src/ty.rs | 16 +++------- 9 files changed, 59 insertions(+), 57 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index 60eaad9b498fc..3469ec4767b7c 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -664,10 +664,7 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> { }); self.types.insert(proj.item_def_id, (infer_ty, proj.substs)); // Recurse into bounds - for pred in self.tcx().bound_explicit_item_bounds(proj.item_def_id).transpose_iter() { - let pred_span = pred.0.1; - - let pred = pred.map_bound(|(pred, _)| *pred).subst(self.tcx(), proj.substs); + for (pred, pred_span) in self.tcx().bound_explicit_item_bounds(proj.item_def_id).subst_iter_copied(self.tcx(), proj.substs) { let pred = pred.fold_with(self); let pred = self.ocx.normalize( ObligationCause::misc(self.span, self.body_id), @@ -1752,15 +1749,10 @@ pub fn check_type_bounds<'tcx>( let obligations = tcx .bound_explicit_item_bounds(trait_ty.def_id) - .transpose_iter() - .map(|e| e.map_bound(|e| *e).transpose_tuple2()) - .map(|(bound, span)| { - debug!(?bound); - // this is where opaque type is found - let concrete_ty_bound = bound.subst(tcx, rebased_substs); + .subst_iter_copied(tcx, rebased_substs) + .map(|(concrete_ty_bound, span)| { debug!("check_type_bounds: concrete_ty_bound = {:?}", concrete_ty_bound); - - traits::Obligation::new(mk_cause(span.0), param_env, concrete_ty_bound) + traits::Obligation::new(mk_cause(span), param_env, concrete_ty_bound) }) .collect(); debug!("check_type_bounds: item_bounds={:?}", obligations); diff --git a/compiler/rustc_hir_typeck/src/_match.rs b/compiler/rustc_hir_typeck/src/_match.rs index c22d828625bc8..2b15d4dcd0848 100644 --- a/compiler/rustc_hir_typeck/src/_match.rs +++ b/compiler/rustc_hir_typeck/src/_match.rs @@ -514,8 +514,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } for ty in [first_ty, second_ty] { - for pred in self.tcx.bound_explicit_item_bounds(rpit_def_id).transpose_iter() { - let pred = pred.map_bound(|(pred, _)| *pred).subst(self.tcx, substs); + for (pred, _) in self + .tcx + .bound_explicit_item_bounds(rpit_def_id) + .subst_iter_copied(self.tcx, substs) + { let pred = match pred.kind().skip_binder() { ty::PredicateKind::Trait(mut trait_pred) => { assert_eq!(trait_pred.trait_ref.self_ty(), opaque_ty); diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index de5b4a5021892..a5a45f75e0e24 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -176,24 +176,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match *expected_ty.kind() { ty::Opaque(def_id, substs) => { let bounds = self.tcx.bound_explicit_item_bounds(def_id); - let sig = bounds - .transpose_iter() - .map(|e| e.map_bound(|e| *e).transpose_tuple2()) - .find_map(|(pred, span)| match pred.0.kind().skip_binder() { + let sig = + bounds.subst_iter_copied(self.tcx, substs).find_map(|(pred, span)| match pred + .kind() + .skip_binder() + { ty::PredicateKind::Projection(proj_predicate) => self .deduce_sig_from_projection( - Some(span.0), - pred.0 - .kind() - .rebind(pred.rebind(proj_predicate).subst(self.tcx, substs)), + Some(span), + pred.kind().rebind(proj_predicate), ), _ => None, }); let kind = bounds - .transpose_iter() - .map(|e| e.map_bound(|e| *e).transpose_tuple2()) - .filter_map(|(pred, _)| match pred.0.kind().skip_binder() { + .0 + .iter() + .filter_map(|(pred, _)| match pred.kind().skip_binder() { ty::PredicateKind::Trait(tp) => { self.tcx.fn_trait_kind_from_lang_item(tp.def_id()) } @@ -697,18 +696,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty::Opaque(def_id, substs) => self .tcx .bound_explicit_item_bounds(def_id) - .transpose_iter() - .map(|e| e.map_bound(|e| *e).transpose_tuple2()) - .find_map(|(p, s)| get_future_output(p.subst(self.tcx, substs), s.0))?, + .subst_iter_copied(self.tcx, substs) + .find_map(|(p, s)| get_future_output(p, s))?, ty::Error(_) => return None, ty::Projection(proj) if self.tcx.def_kind(proj.item_def_id) == DefKind::ImplTraitPlaceholder => { self.tcx .bound_explicit_item_bounds(proj.item_def_id) - .transpose_iter() - .map(|e| e.map_bound(|e| *e).transpose_tuple2()) - .find_map(|(p, s)| get_future_output(p.subst(self.tcx, proj.substs), s.0))? + .subst_iter_copied(self.tcx, proj.substs) + .find_map(|(p, s)| get_future_output(p, s))? } _ => span_bug!( self.tcx.def_span(expr_def_id), diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index ddeeaa9618e60..2fe6f63a9c639 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -338,8 +338,7 @@ impl<'tcx> InferCtxt<'tcx> { let bounds = self.tcx.bound_explicit_item_bounds(*def_id); - for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) { - let predicate = predicate.subst(self.tcx, substs); + for (predicate, _) in bounds.subst_iter_copied(self.tcx, substs) { let output = predicate .kind() .map_bound(|kind| match kind { diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types.rs index 77e8f72aefac0..80ecd737829e6 100644 --- a/compiler/rustc_infer/src/infer/opaque_types.rs +++ b/compiler/rustc_infer/src/infer/opaque_types.rs @@ -543,10 +543,7 @@ impl<'tcx> InferCtxt<'tcx> { let item_bounds = tcx.bound_explicit_item_bounds(def_id.to_def_id()); - for predicate in item_bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) { - debug!(?predicate); - let predicate = predicate.subst(tcx, substs); - + for (predicate, _) in item_bounds.subst_iter_copied(tcx, substs) { let predicate = predicate.fold_with(&mut BottomUpFolder { tcx, ty_op: |ty| match *ty.kind() { diff --git a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs index 81b9f55e7033a..e0958b039dc56 100644 --- a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs +++ b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs @@ -91,14 +91,12 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound { // For example, in `impl Trait`, for all of the bounds on `Assoc`, // e.g. `type Assoc: OtherTrait`, replace `::Assoc: OtherTrait` // with `impl Send: OtherTrait`. - for assoc_pred_and_span in - cx.tcx.bound_explicit_item_bounds(proj.projection_ty.item_def_id).transpose_iter() + for (assoc_pred, assoc_pred_span) in cx + .tcx + .bound_explicit_item_bounds(proj.projection_ty.item_def_id) + .subst_iter_copied(cx.tcx, &proj.projection_ty.substs) { - let assoc_pred_span = assoc_pred_and_span.0.1; - let assoc_pred = assoc_pred_and_span - .map_bound(|(pred, _)| *pred) - .subst(cx.tcx, &proj.projection_ty.substs) - .fold_with(proj_replacer); + let assoc_pred = assoc_pred.fold_with(proj_replacer); let Ok(assoc_pred) = traits::fully_normalize(infcx, traits::ObligationCause::dummy(), cx.param_env, assoc_pred) else { continue; }; diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 66354196b4e51..93d8797553fdb 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -795,8 +795,7 @@ pub trait PrettyPrinter<'tcx>: let mut fn_traits = FxIndexMap::default(); let mut is_sized = false; - for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) { - let predicate = predicate.subst(tcx, substs); + for (predicate, _) in bounds.subst_iter_copied(tcx, substs) { let bound_predicate = predicate.kind(); match bound_predicate.skip_binder() { diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index c2a83ca9dbb83..e0493f45067af 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -6,6 +6,7 @@ use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts}; use crate::ty::visit::{TypeVisitable, TypeVisitor}; use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt}; +use rustc_data_structures::captures::Captures; use rustc_data_structures::intern::{Interned, WithStableHash}; use rustc_hir::def_id::DefId; use rustc_macros::HashStable; @@ -550,6 +551,28 @@ impl EarlyBinder<(T, U)> { } } +impl<'tcx, 's, T: IntoIterator, I: TypeFoldable<'tcx>> EarlyBinder { + pub fn subst_iter( + self, + tcx: TyCtxt<'tcx>, + substs: &'s [GenericArg<'tcx>], + ) -> impl Iterator + Captures<'s> + Captures<'tcx> { + self.0.into_iter().map(move |t| EarlyBinder(t).subst(tcx, substs)) + } +} + +impl<'tcx, 's, 'a, T: IntoIterator, I: Copy + TypeFoldable<'tcx> + 'a> + EarlyBinder +{ + pub fn subst_iter_copied( + self, + tcx: TyCtxt<'tcx>, + substs: &'s [GenericArg<'tcx>], + ) -> impl Iterator + Captures<'s> + Captures<'tcx> + Captures<'a> { + self.0.into_iter().map(move |t| EarlyBinder(*t).subst(tcx, substs)) + } +} + pub struct EarlyBinderIter { t: T, } diff --git a/src/tools/clippy/clippy_utils/src/ty.rs b/src/tools/clippy/clippy_utils/src/ty.rs index a15daec7c3ce3..3b5a9ba83568c 100644 --- a/src/tools/clippy/clippy_utils/src/ty.rs +++ b/src/tools/clippy/clippy_utils/src/ty.rs @@ -657,21 +657,18 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> O let mut output = None; let lang_items = cx.tcx.lang_items(); - for pred in cx + for (pred, _) in cx .tcx .bound_explicit_item_bounds(ty.item_def_id) - .transpose_iter() - .map(|x| x.map_bound(|(p, _)| p)) + .subst_iter_copied(cx.tcx, ty.substs) { - match pred.0.kind().skip_binder() { + match pred.kind().skip_binder() { PredicateKind::Trait(p) if (lang_items.fn_trait() == Some(p.def_id()) || lang_items.fn_mut_trait() == Some(p.def_id()) || lang_items.fn_once_trait() == Some(p.def_id())) => { - let i = pred - .map_bound(|pred| pred.kind().rebind(p.trait_ref.substs.type_at(1))) - .subst(cx.tcx, ty.substs); + let i = pred.kind().rebind(p.trait_ref.substs.type_at(1)); if inputs.map_or(false, |inputs| inputs != i) { // Multiple different fn trait impls. Is this even allowed? @@ -684,10 +681,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> O // Multiple different fn trait impls. Is this even allowed? return None; } - output = Some( - pred.map_bound(|pred| pred.kind().rebind(p.term.ty().unwrap())) - .subst(cx.tcx, ty.substs), - ); + output = pred.kind().rebind(p.term.ty()).transpose(); }, _ => (), } From 62b65b5edeaf87a7889c84c61218878f4849e8da Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Sat, 22 Oct 2022 07:08:45 -0400 Subject: [PATCH 12/14] Change `unknown_lint` applicability to `MaybeIncorrect` --- compiler/rustc_lint/src/levels.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index d3879ff487de9..db0a3419e6a5d 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -960,7 +960,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> { sp, "did you mean", suggestion, - Applicability::MachineApplicable, + Applicability::MaybeIncorrect, ); } lint From 94a9ee3692d43627aecc2b0889134d4b71e46257 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 22 Oct 2022 14:12:01 +0200 Subject: [PATCH 13/14] Use functions for headings rustodoc GUI test --- src/test/rustdoc-gui/headings.goml | 176 +++++++++++++---------------- 1 file changed, 76 insertions(+), 100 deletions(-) diff --git a/src/test/rustdoc-gui/headings.goml b/src/test/rustdoc-gui/headings.goml index 9a77d8bbd1541..85e17ca9551a2 100644 --- a/src/test/rustdoc-gui/headings.goml +++ b/src/test/rustdoc-gui/headings.goml @@ -150,109 +150,85 @@ assert-css: ("h2#top-doc-prose-title", {"border-bottom-width": "1px"}) assert-css: ("h3#top-doc-prose-sub-heading", {"font-size": "20px"}) assert-css: ("h3#top-doc-prose-sub-heading", {"border-bottom-width": "1px"}) -// Checking colors now. +// Needed to check colors show-text: true -local-storage: {"rustdoc-theme": "light", "rustdoc-use-system-theme": "false"} goto: "file://" + |DOC_PATH| + "/test_docs/struct.HeavilyDocumentedStruct.html" -assert-css: ( - ".top-doc .docblock h2", - {"color": "rgb(0, 0, 0)", "border-bottom": "1px solid rgb(221, 221, 221)"}, -) -assert-css: ( - ".top-doc .docblock h3", - {"color": "rgb(0, 0, 0)", "border-bottom": "1px solid rgb(221, 221, 221)"}, -) -assert-css: ( - ".top-doc .docblock h4", - {"color": "rgb(0, 0, 0)", "border-bottom": "1px solid rgb(221, 221, 221)"}, -) -assert-css: ( - ".top-doc .docblock h5", - {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h4", - {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h5", - {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h6", - {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, -) -local-storage: {"rustdoc-theme": "dark"} -reload: -assert-css: ( - ".top-doc .docblock h2", - {"color": "rgb(221, 221, 221)", "border-bottom": "1px solid rgb(210, 210, 210)"}, -) -assert-css: ( - ".top-doc .docblock h3", - {"color": "rgb(221, 221, 221)", "border-bottom": "1px solid rgb(210, 210, 210)"}, -) -assert-css: ( - ".top-doc .docblock h4", - {"color": "rgb(221, 221, 221)", "border-bottom": "1px solid rgb(210, 210, 210)"}, -) -assert-css: ( - ".top-doc .docblock h5", - {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h4", - {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h5", - {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h6", - {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, +define-function: ( + "check-colors", + (theme, heading_color, small_heading_color, heading_border_color), + [ + ("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}), + ("reload"), + ("assert-css", ( + ".top-doc .docblock h2", + {"color": |heading_color|, "border-bottom": "1px solid " + |heading_border_color|}, + )), + ("assert-css", ( + ".top-doc .docblock h3", + {"color": |heading_color|, "border-bottom": "1px solid " + |heading_border_color|}, + )), + ("assert-css", ( + ".top-doc .docblock h4", + {"color": |heading_color|, "border-bottom": "1px solid " + |heading_border_color|}, + )), + ("assert-css", ( + ".top-doc .docblock h5", + {"color": |small_heading_color|, "border-bottom-width": "0px"}, + )), + ("assert-css", ( + "#implementations-list .docblock h4", + {"color": |heading_color|, "border-bottom-width": "0px"}, + )), + ("assert-css", ( + "#implementations-list .docblock h5", + {"color": |small_heading_color|, "border-bottom-width": "0px"}, + )), + ("assert-css", ( + "#implementations-list .docblock h6", + {"color": |small_heading_color|, "border-bottom-width": "0px"}, + )), + ], +) +call-function: ( + "check-colors", + { + "theme": "ayu", + "heading_color": "rgb(255, 255, 255)", + "small_heading_color": "rgb(197, 197, 197)", + "heading_border_color": "rgb(92, 103, 115)", + }, +) +call-function: ( + "check-colors", + { + "theme": "dark", + "heading_color": "rgb(221, 221, 221)", + "small_heading_color": "rgb(221, 221, 221)", + "heading_border_color": "rgb(210, 210, 210)", + }, +) +call-function: ( + "check-colors", + { + "theme": "light", + "heading_color": "rgb(0, 0, 0)", + "small_heading_color": "rgb(0, 0, 0)", + "heading_border_color": "rgb(221, 221, 221)", + }, +) + +define-function: ( + "check-since-color", + (theme), + [ + ("local-storage", {"rustdoc-theme": |theme|}), + ("reload"), + ("assert-css", (".since", {"color": "rgb(128, 128, 128)"}, ALL)), + ], ) -local-storage: {"rustdoc-theme": "ayu"} -reload: -assert-css: ( - ".top-doc .docblock h2", - {"color": "rgb(255, 255, 255)", "border-bottom": "1px solid rgb(92, 103, 115)"}, -) -assert-css: ( - ".top-doc .docblock h2", - {"color": "rgb(255, 255, 255)", "border-bottom": "1px solid rgb(92, 103, 115)"}, -) -assert-css: ( - ".top-doc .docblock h4", - {"color": "rgb(255, 255, 255)", "border-bottom": "1px solid rgb(92, 103, 115)"}, -) -assert-css: ( - ".top-doc .docblock h5", - {"color": "rgb(197, 197, 197)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h4", - {"color": "rgb(255, 255, 255)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h5", - {"color": "rgb(197, 197, 197)", "border-bottom-width": "0px"}, -) -assert-css: ( - "#implementations-list .docblock h6", - {"color": "rgb(197, 197, 197)", "border-bottom-width": "0px"}, -) - -local-storage: {"rustdoc-theme": "light"} goto: "file://" + |DOC_PATH| + "/staged_api/struct.Foo.html" -assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL) - -local-storage: {"rustdoc-theme": "dark"} -reload: -assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL) - -local-storage: {"rustdoc-theme": "ayu"} -reload: -assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL) +call-function: ("check-since-color", ("ayu")) +call-function: ("check-since-color", ("dark")) +call-function: ("check-since-color", ("light")) From 9f0503e4a60d73b6a8404e4f9c7678e162173305 Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Sat, 22 Oct 2022 12:26:47 -0700 Subject: [PATCH 14/14] Fix typo in docs of `String::leak`. --- library/alloc/src/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 209abfac6bbf9..c436adf70067a 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1851,7 +1851,7 @@ impl String { } /// Consumes and leaks the `String`, returning a mutable reference to the contents, - /// `&'a mut str`. + /// `&'static mut str`. /// /// This is mainly useful for data that lives for the remainder of /// the program's life. Dropping the returned reference will cause a memory