diff options
| author | bors <bors@rust-lang.org> | 2024-08-13 15:01:50 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-08-13 15:01:50 +0000 |
| commit | f96e296927cc0c6e9dd611edb943f6349001eca5 (patch) | |
| tree | 76c5e72c0570998ece04f11ac90e09b5d2613abc /compiler/rustc_trait_selection/src | |
| parent | 00423bb1d85f3513e534abdb26f54a6966e88d47 (diff) | |
| parent | 28af7e09581c3b39cdbf2850df2f157690ab7e56 (diff) | |
| download | rust-f96e296927cc0c6e9dd611edb943f6349001eca5.tar.gz rust-f96e296927cc0c6e9dd611edb943f6349001eca5.zip | |
Auto merge of #17880 - lnicola:sync-from-rust, r=lnicola
minor: sync from downstream
Diffstat (limited to 'compiler/rustc_trait_selection/src')
55 files changed, 654 insertions, 553 deletions
diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index daabdec8f9e..8ccb2a8483a 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -60,12 +60,11 @@ use rustc_hir::{self as hir}; use rustc_macros::extension; use rustc_middle::bug; use rustc_middle::dep_graph::DepContext; -use rustc_middle::ty::error::ExpectedFound; -use rustc_middle::ty::error::TypeErrorToStringExt; +use rustc_middle::ty::error::{ExpectedFound, TypeError, TypeErrorToStringExt}; use rustc_middle::ty::print::{with_forced_trimmed_paths, PrintError, PrintTraitRefExt as _}; use rustc_middle::ty::{ - self, error::TypeError, List, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, - TypeVisitable, TypeVisitableExt, + self, List, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable, + TypeVisitableExt, }; use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span}; use rustc_target::spec::abi; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index 56ea70bcf1d..f6dd7898fb2 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -1,13 +1,11 @@ -use crate::error_reporting::TypeErrCtxt; -use crate::errors::{ - AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError, - SourceKindMultiSuggestion, SourceKindSubdiag, -}; -use crate::infer::InferCtxt; -use rustc_errors::{codes::*, Diag, IntoDiagArg}; +use std::borrow::Cow; +use std::iter; +use std::path::PathBuf; + +use rustc_errors::codes::*; +use rustc_errors::{Diag, IntoDiagArg}; use rustc_hir as hir; -use rustc_hir::def::Res; -use rustc_hir::def::{CtorOf, DefKind, Namespace}; +use rustc_hir::def::{CtorOf, DefKind, Namespace, Res}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, LetStmt, LocalSource}; @@ -21,9 +19,13 @@ use rustc_middle::ty::{ }; use rustc_span::symbol::{sym, Ident}; use rustc_span::{BytePos, Span, DUMMY_SP}; -use std::borrow::Cow; -use std::iter; -use std::path::PathBuf; + +use crate::error_reporting::TypeErrCtxt; +use crate::errors::{ + AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError, + SourceKindMultiSuggestion, SourceKindSubdiag, +}; +use crate::infer::InferCtxt; pub enum TypeAnnotationNeeded { /// ```compile_fail,E0282 @@ -932,13 +934,13 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { // which makes this somewhat difficult and prevents us from just // using `self.path_inferred_arg_iter` here. hir::ExprKind::Struct(&hir::QPath::Resolved(_self_ty, path), _, _) - // FIXME(TaKO8Ki): Ideally we should support this. For that - // we have to map back from the self type to the - // type alias though. That's difficult. + // FIXME(TaKO8Ki): Ideally we should support other kinds, + // such as `TyAlias` or `AssocTy`. For that we have to map + // back from the self type to the type alias though. That's difficult. // // See the `need_type_info/issue-103053.rs` test for // a example. - if !matches!(path.res, Res::Def(DefKind::TyAlias, _)) => { + if matches!(path.res, Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) => { if let Some(ty) = self.opt_node_type(expr.hir_id) && let ty::Adt(_, args) = ty.kind() { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/different_lifetimes.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/different_lifetimes.rs index 74dcde03639..8f84d771216 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/different_lifetimes.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/different_lifetimes.rs @@ -1,21 +1,17 @@ //! Error Reporting for Anonymous Region Lifetime Errors //! where both the regions are anonymous. -use crate::error_reporting::infer::nice_region_error::find_anon_type::find_anon_type; -use crate::error_reporting::infer::nice_region_error::util::AnonymousParamInfo; -use crate::error_reporting::infer::nice_region_error::NiceRegionError; -use crate::errors::AddLifetimeParamsSuggestion; -use crate::errors::LifetimeMismatch; -use crate::errors::LifetimeMismatchLabels; -use crate::infer::RegionResolutionError; -use crate::infer::SubregionOrigin; - -use rustc_errors::Subdiagnostic; -use rustc_errors::{Diag, ErrorGuaranteed}; +use rustc_errors::{Diag, ErrorGuaranteed, Subdiagnostic}; use rustc_hir::def_id::LocalDefId; use rustc_hir::Ty; use rustc_middle::ty::{Region, TyCtxt}; +use crate::error_reporting::infer::nice_region_error::find_anon_type::find_anon_type; +use crate::error_reporting::infer::nice_region_error::util::AnonymousParamInfo; +use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::errors::{AddLifetimeParamsSuggestion, LifetimeMismatch, LifetimeMismatchLabels}; +use crate::infer::{RegionResolutionError, SubregionOrigin}; + impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { /// Print the error message for lifetime errors when both the concerned regions are anonymous. /// diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs index a892ce58861..3f35391be13 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs @@ -1,4 +1,5 @@ use core::ops::ControlFlow; + use rustc_hir as hir; use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::{self, Visitor}; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs index 550cc455e01..221f6675d22 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mismatched_static_lifetime.rs @@ -1,14 +1,6 @@ //! Error Reporting for when the lifetime for a type doesn't match the `impl` selected for a predicate //! to hold. -use crate::error_reporting::infer::nice_region_error::NiceRegionError; -use crate::errors::{note_and_explain, IntroducesStaticBecauseUnmetLifetimeReq}; -use crate::errors::{ - DoesNotOutliveStaticFromImpl, ImplicitStaticLifetimeSubdiag, MismatchedStaticLifetime, -}; -use crate::infer::RegionResolutionError; -use crate::infer::{SubregionOrigin, TypeTrace}; -use crate::traits::ObligationCauseCode; use rustc_data_structures::fx::FxIndexSet; use rustc_errors::{ErrorGuaranteed, MultiSpan}; use rustc_hir as hir; @@ -16,6 +8,14 @@ use rustc_hir::intravisit::Visitor; use rustc_middle::bug; use rustc_middle::ty::TypeVisitor; +use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::errors::{ + note_and_explain, DoesNotOutliveStaticFromImpl, ImplicitStaticLifetimeSubdiag, + IntroducesStaticBecauseUnmetLifetimeReq, MismatchedStaticLifetime, +}; +use crate::infer::{RegionResolutionError, SubregionOrigin, TypeTrace}; +use crate::traits::ObligationCauseCode; + impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { pub(super) fn try_report_mismatched_static_lifetime(&self) -> Option<ErrorGuaranteed> { let error = self.error.as_ref()?; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mod.rs index b83ecd8320c..79a770ac9b3 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/mod.rs @@ -1,11 +1,12 @@ -use crate::error_reporting::TypeErrCtxt; -use crate::infer::RegionResolutionError; -use crate::infer::RegionResolutionError::*; use rustc_errors::{Diag, ErrorGuaranteed}; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::{self, TyCtxt}; use rustc_span::Span; +use crate::error_reporting::TypeErrCtxt; +use crate::infer::RegionResolutionError; +use crate::infer::RegionResolutionError::*; + mod different_lifetimes; pub mod find_anon_type; mod mismatched_static_lifetime; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs index d1802d2f5ee..f91a81f76f4 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs @@ -1,13 +1,14 @@ //! Error Reporting for Anonymous Region Lifetime Errors //! where one region is named and the other is anonymous. -use crate::error_reporting::infer::nice_region_error::find_anon_type::find_anon_type; -use crate::error_reporting::infer::nice_region_error::NiceRegionError; -use crate::errors::ExplicitLifetimeRequired; use rustc_errors::Diag; use rustc_middle::ty; use rustc_span::symbol::kw; +use crate::error_reporting::infer::nice_region_error::find_anon_type::find_anon_type; +use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::errors::ExplicitLifetimeRequired; + impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { /// When given a `ConcreteFailure` for a function with parameters containing a named region and /// an anonymous region, emit an descriptive diagnostic error. diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs index 476ac3f1720..8da0edbeb02 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs @@ -1,12 +1,5 @@ -use crate::error_reporting::infer::nice_region_error::NiceRegionError; -use crate::errors::{ - ActualImplExpectedKind, ActualImplExpectedLifetimeKind, ActualImplExplNotes, - TraitPlaceholderMismatch, TyOrSig, -}; -use crate::infer::RegionResolutionError; -use crate::infer::ValuePairs; -use crate::infer::{SubregionOrigin, TypeTrace}; -use crate::traits::{ObligationCause, ObligationCauseCode}; +use std::fmt; + use rustc_data_structures::intern::Interned; use rustc_errors::{Diag, IntoDiagArg}; use rustc_hir::def::Namespace; @@ -14,10 +7,15 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_ID}; use rustc_middle::bug; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::print::{FmtPrinter, Print, PrintTraitRefExt as _, RegionHighlightMode}; -use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::{self, RePlaceholder, Region, TyCtxt}; +use rustc_middle::ty::{self, GenericArgsRef, RePlaceholder, Region, TyCtxt}; -use std::fmt; +use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::errors::{ + ActualImplExpectedKind, ActualImplExpectedLifetimeKind, ActualImplExplNotes, + TraitPlaceholderMismatch, TyOrSig, +}; +use crate::infer::{RegionResolutionError, SubregionOrigin, TypeTrace, ValuePairs}; +use crate::traits::{ObligationCause, ObligationCauseCode}; // HACK(eddyb) maybe move this in a more central location. #[derive(Copy, Clone)] diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_relation.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_relation.rs index e9f17a3e3e2..9c772f42cca 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_relation.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_relation.rs @@ -1,10 +1,11 @@ -use crate::error_reporting::infer::nice_region_error::NiceRegionError; -use crate::errors::PlaceholderRelationLfNotSatisfied; -use crate::infer::{RegionResolutionError, SubregionOrigin}; use rustc_data_structures::intern::Interned; use rustc_errors::Diag; use rustc_middle::ty::{self, RePlaceholder, Region}; +use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::errors::PlaceholderRelationLfNotSatisfied; +use crate::infer::{RegionResolutionError, SubregionOrigin}; + impl<'tcx> NiceRegionError<'_, 'tcx> { /// Emitted wwhen given a `ConcreteFailure` when relating two placeholders. pub(super) fn try_report_placeholder_relation(&self) -> Option<Diag<'tcx>> { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs index e5d97976534..dc775b824da 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs @@ -1,13 +1,5 @@ //! Error Reporting for static impl Traits. -use crate::error_reporting::infer::nice_region_error::NiceRegionError; -use crate::errors::{ - ButCallingIntroduces, ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted, - ReqIntroducedLocations, -}; -use crate::infer::RegionResolutionError; -use crate::infer::{SubregionOrigin, TypeTrace}; -use crate::traits::{ObligationCauseCode, UnifyReceiverContext}; use rustc_data_structures::fx::FxIndexSet; use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, Subdiagnostic}; use rustc_hir::def_id::DefId; @@ -19,10 +11,17 @@ use rustc_hir::{ use rustc_middle::ty::{ self, AssocItemContainer, StaticLifetimeVisitor, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor, }; +use rustc_span::def_id::LocalDefId; use rustc_span::symbol::Ident; use rustc_span::Span; -use rustc_span::def_id::LocalDefId; +use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::errors::{ + ButCallingIntroduces, ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted, + ReqIntroducedLocations, +}; +use crate::infer::{RegionResolutionError, SubregionOrigin, TypeTrace}; +use crate::traits::{ObligationCauseCode, UnifyReceiverContext}; impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { /// Print the error message for lifetime errors when the return type is a static `impl Trait`, diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs index c58c7e13551..09af00beba7 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs @@ -1,9 +1,5 @@ //! Error Reporting for `impl` items that do not match the obligations from their `trait`. -use crate::error_reporting::infer::nice_region_error::NiceRegionError; -use crate::errors::{ConsiderBorrowingParamHelp, RelationshipHelp, TraitImplDiff}; -use crate::infer::RegionResolutionError; -use crate::infer::{Subtype, ValuePairs}; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir::def::Res; @@ -16,6 +12,10 @@ use rustc_middle::ty::print::RegionHighlightMode; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor}; use rustc_span::Span; +use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::errors::{ConsiderBorrowingParamHelp, RelationshipHelp, TraitImplDiff}; +use crate::infer::{RegionResolutionError, Subtype, ValuePairs}; + impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { /// Print the error message for lifetime errors when the `impl` doesn't conform to the `trait`. pub(super) fn try_report_impl_not_conforming_to_trait(&self) -> Option<ErrorGuaranteed> { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note.rs index aeb3049c2ae..04e1be22a4d 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note.rs @@ -1,10 +1,3 @@ -use crate::error_reporting::infer::{note_and_explain_region, TypeErrCtxt}; -use crate::errors::{ - note_and_explain, FulfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent, - RefLongerThanData, RegionOriginNote, WhereClauseSuggestions, -}; -use crate::fluent_generated as fluent; -use crate::infer::{self, SubregionOrigin}; use rustc_errors::{Diag, Subdiagnostic}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::traits::ObligationCauseCode; @@ -13,6 +6,13 @@ use rustc_middle::ty::{self, IsSuggestable, Region, Ty}; use rustc_span::symbol::kw; use super::ObligationCauseAsDiagArg; +use crate::error_reporting::infer::{note_and_explain_region, TypeErrCtxt}; +use crate::errors::{ + note_and_explain, FulfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent, + RefLongerThanData, RegionOriginNote, WhereClauseSuggestions, +}; +use crate::fluent_generated as fluent; +use crate::infer::{self, SubregionOrigin}; impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { pub(super) fn note_region_origin(&self, err: &mut Diag<'_>, origin: &SubregionOrigin<'tcx>) { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs index f9110cfb3b9..864510bb650 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs @@ -2,14 +2,12 @@ use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect}; use rustc_errors::{pluralize, Diag, MultiSpan}; use rustc_hir as hir; use rustc_hir::def::DefKind; -use rustc_middle::traits::ObligationCauseCode; -use rustc_middle::ty::error::ExpectedFound; -use rustc_middle::ty::print::Printer; -use rustc_middle::{ - traits::ObligationCause, - ty::{self, error::TypeError, print::FmtPrinter, suggest_constraining_type_param, Ty}, -}; -use rustc_span::{def_id::DefId, sym, BytePos, Span, Symbol}; +use rustc_middle::traits::{ObligationCause, ObligationCauseCode}; +use rustc_middle::ty::error::{ExpectedFound, TypeError}; +use rustc_middle::ty::print::{FmtPrinter, Printer}; +use rustc_middle::ty::{self, suggest_constraining_type_param, Ty}; +use rustc_span::def_id::DefId; +use rustc_span::{sym, BytePos, Span, Symbol}; use crate::error_reporting::TypeErrCtxt; use crate::infer::InferCtxtExt; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs index 1ef32d110b3..ee159aa0b77 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs @@ -1,5 +1,5 @@ -use crate::error_reporting::infer::hir::Path; use core::ops::ControlFlow; + use hir::def::CtorKind; use hir::intravisit::{walk_expr, walk_stmt, Visitor}; use hir::{LetStmt, QPath}; @@ -7,8 +7,7 @@ use rustc_data_structures::fx::FxIndexSet; use rustc_errors::{Applicability, Diag}; use rustc_hir as hir; use rustc_hir::def::Res; -use rustc_hir::MatchSource; -use rustc_hir::Node; +use rustc_hir::{MatchSource, Node}; use rustc_middle::traits::{ IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode, StatementAsExpression, @@ -17,6 +16,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt}; use rustc_span::{sym, Span}; +use crate::error_reporting::infer::hir::Path; use crate::error_reporting::TypeErrCtxt; use crate::errors::{ ConsiderAddingAwait, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes, diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs index 72a4d4c1205..9ab47057859 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs @@ -388,39 +388,67 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { trait_impls.non_blanket_impls().values().flatten().count(); // If there is only one implementation of the trait, suggest using it. // Otherwise, use a placeholder comment for the implementation. - let (message, self_type) = if non_blanket_impl_count == 1 { + let (message, self_types) = if non_blanket_impl_count == 1 { ( "use the fully-qualified path to the only available \ implementation", - format!( + vec![format!( "{}", self.tcx.type_of(impl_def_id).instantiate_identity() - ), + )], + ) + } else if non_blanket_impl_count < 20 { + ( + "use a fully-qualified path to one of the available \ + implementations", + trait_impls + .non_blanket_impls() + .values() + .flatten() + .map(|id| { + format!( + "{}", + self.tcx.type_of(id).instantiate_identity() + ) + }) + .collect::<Vec<String>>(), ) } else { ( "use a fully-qualified path to a specific available \ implementation", - "/* self type */".to_string(), + vec!["/* self type */".to_string()], ) }; - let mut suggestions = - vec![(path.span.shrink_to_lo(), format!("<{self_type} as "))]; - if let Some(generic_arg) = trait_path_segment.args { - let between_span = - trait_path_segment.ident.span.between(generic_arg.span_ext); - // get rid of :: between Trait and <type> - // must be '::' between them, otherwise the parser won't accept the code - suggestions.push((between_span, "".to_string())); - suggestions - .push((generic_arg.span_ext.shrink_to_hi(), ">".to_string())); - } else { - suggestions.push(( - trait_path_segment.ident.span.shrink_to_hi(), - ">".to_string(), - )); - } - err.multipart_suggestion( + let suggestions: Vec<_> = self_types + .into_iter() + .map(|self_type| { + let mut suggestions = vec![( + path.span.shrink_to_lo(), + format!("<{self_type} as "), + )]; + if let Some(generic_arg) = trait_path_segment.args { + let between_span = trait_path_segment + .ident + .span + .between(generic_arg.span_ext); + // get rid of :: between Trait and <type> + // must be '::' between them, otherwise the parser won't accept the code + suggestions.push((between_span, "".to_string())); + suggestions.push(( + generic_arg.span_ext.shrink_to_hi(), + ">".to_string(), + )); + } else { + suggestions.push(( + trait_path_segment.ident.span.shrink_to_hi(), + ">".to_string(), + )); + } + suggestions + }) + .collect(); + err.multipart_suggestions( message, suggestions, Applicability::MaybeIncorrect, diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index a7ea308a818..f908d8a6870 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -1,31 +1,17 @@ -use super::on_unimplemented::{AppendConstMessage, OnUnimplementedNote}; -use super::suggestions::get_explanation_based_on_obligation; -use crate::error_reporting::infer::TyCategory; -use crate::error_reporting::traits::report_object_safety_error; -use crate::error_reporting::TypeErrCtxt; -use crate::errors::{ - AsyncClosureNotFn, ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch, -}; -use crate::infer::InferCtxtExt as _; -use crate::infer::{self, InferCtxt}; -use crate::traits::query::evaluate_obligation::InferCtxtExt as _; -use crate::traits::NormalizeExt; -use crate::traits::{ - elaborate, MismatchedProjectionTypes, Obligation, ObligationCause, ObligationCauseCode, - ObligationCtxt, Overflow, PredicateObligation, SelectionError, SignatureMismatch, - TraitNotObjectSafe, -}; use core::ops::ControlFlow; +use std::borrow::Cow; + use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::unord::UnordSet; use rustc_errors::codes::*; -use rustc_errors::{pluralize, struct_span_code_err, Applicability, StringPart}; -use rustc_errors::{Diag, ErrorGuaranteed, StashKey}; +use rustc_errors::{ + pluralize, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey, + StringPart, +}; use rustc_hir::def::Namespace; -use rustc_hir::def_id::{DefId, LocalDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::intravisit::Visitor; -use rustc_hir::Node; -use rustc_hir::{self as hir, LangItem}; +use rustc_hir::{self as hir, LangItem, Node}; use rustc_infer::infer::{InferOk, TypeTrace}; use rustc_middle::traits::select::OverflowError; use rustc_middle::traits::SignatureMismatchData; @@ -42,11 +28,25 @@ use rustc_middle::ty::{ use rustc_middle::{bug, span_bug}; use rustc_span::symbol::sym; use rustc_span::{BytePos, Span, Symbol, DUMMY_SP}; -use std::borrow::Cow; +use super::on_unimplemented::{AppendConstMessage, OnUnimplementedNote}; +use super::suggestions::get_explanation_based_on_obligation; use super::{ ArgKind, CandidateSimilarity, GetSafeTransmuteErrorAndReason, ImplCandidate, UnsatisfiedConst, }; +use crate::error_reporting::infer::TyCategory; +use crate::error_reporting::traits::report_object_safety_error; +use crate::error_reporting::TypeErrCtxt; +use crate::errors::{ + AsyncClosureNotFn, ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch, +}; +use crate::infer::{self, InferCtxt, InferCtxtExt as _}; +use crate::traits::query::evaluate_obligation::InferCtxtExt as _; +use crate::traits::{ + elaborate, MismatchedProjectionTypes, NormalizeExt, Obligation, ObligationCause, + ObligationCauseCode, ObligationCtxt, Overflow, PredicateObligation, SelectionError, + SignatureMismatch, TraitNotObjectSafe, +}; impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { /// The `root_obligation` parameter should be the `root_obligation` field @@ -687,10 +687,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let mut applied_do_not_recommend = false; loop { if let ObligationCauseCode::ImplDerived(ref c) = base_cause { - if self.tcx.has_attrs_with_path( - c.impl_or_alias_def_id, - &[sym::diagnostic, sym::do_not_recommend], - ) { + if self.tcx.do_not_recommend_impl(c.impl_or_alias_def_id) { let code = (*c.derived.parent_code).clone(); obligation.cause.map_code(|_| code); obligation.predicate = c.derived.parent_trait_pred.upcast(self.tcx); @@ -944,8 +941,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // The current method call returns `Result<_, ()>` && self.can_eq(obligation.param_env, ty, found_ty) // There's a single argument in the method call and it is a closure - && args.len() == 1 - && let Some(arg) = args.get(0) + && let [arg] = args && let hir::ExprKind::Closure(closure) = arg.kind // The closure has a block for its body with no tail expression && let body = self.tcx.hir().body(closure.body) @@ -1625,9 +1621,127 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { other: bool, param_env: ty::ParamEnv<'tcx>, ) -> bool { - // If we have a single implementation, try to unify it with the trait ref - // that failed. This should uncover a better hint for what *is* implemented. + let alternative_candidates = |def_id: DefId| { + let mut impl_candidates: Vec<_> = self + .tcx + .all_impls(def_id) + // ignore `do_not_recommend` items + .filter(|def_id| !self.tcx.do_not_recommend_impl(*def_id)) + // Ignore automatically derived impls and `!Trait` impls. + .filter_map(|def_id| self.tcx.impl_trait_header(def_id)) + .filter_map(|header| { + (header.polarity != ty::ImplPolarity::Negative + || self.tcx.is_automatically_derived(def_id)) + .then(|| header.trait_ref.instantiate_identity()) + }) + .filter(|trait_ref| { + let self_ty = trait_ref.self_ty(); + // Avoid mentioning type parameters. + if let ty::Param(_) = self_ty.kind() { + false + } + // Avoid mentioning types that are private to another crate + else if let ty::Adt(def, _) = self_ty.peel_refs().kind() { + // FIXME(compiler-errors): This could be generalized, both to + // be more granular, and probably look past other `#[fundamental]` + // types, too. + self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx) + } else { + true + } + }) + .collect(); + + impl_candidates.sort_by_key(|tr| tr.to_string()); + impl_candidates.dedup(); + impl_candidates + }; + + // We'll check for the case where the reason for the mismatch is that the trait comes from + // one crate version and the type comes from another crate version, even though they both + // are from the same crate. + let trait_def_id = trait_ref.def_id(); + if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind() + && let found_type = def.did() + && trait_def_id.krate != found_type.krate + && self.tcx.crate_name(trait_def_id.krate) == self.tcx.crate_name(found_type.krate) + { + let name = self.tcx.crate_name(trait_def_id.krate); + let spans: Vec<_> = [trait_def_id, found_type] + .into_iter() + .filter_map(|def_id| self.tcx.extern_crate(def_id)) + .map(|data| { + let dependency = if data.dependency_of == LOCAL_CRATE { + "direct dependency of the current crate".to_string() + } else { + let dep = self.tcx.crate_name(data.dependency_of); + format!("dependency of crate `{dep}`") + }; + ( + data.span, + format!("one version of crate `{name}` is used here, as a {dependency}"), + ) + }) + .collect(); + let mut span: MultiSpan = spans.iter().map(|(sp, _)| *sp).collect::<Vec<Span>>().into(); + for (sp, label) in spans.into_iter() { + span.push_span_label(sp, label); + } + err.highlighted_span_help( + span, + vec![ + StringPart::normal("you have ".to_string()), + StringPart::highlighted("multiple different versions".to_string()), + StringPart::normal(" of crate `".to_string()), + StringPart::highlighted(format!("{name}")), + StringPart::normal("` in your dependency graph".to_string()), + ], + ); + let candidates = if impl_candidates.is_empty() { + alternative_candidates(trait_def_id) + } else { + impl_candidates.into_iter().map(|cand| cand.trait_ref).collect() + }; + if let Some((sp_candidate, sp_found)) = candidates.iter().find_map(|trait_ref| { + if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind() + && let candidate_def_id = def.did() + && let Some(name) = self.tcx.opt_item_name(candidate_def_id) + && let Some(found) = self.tcx.opt_item_name(found_type) + && name == found + && candidate_def_id.krate != found_type.krate + && self.tcx.crate_name(candidate_def_id.krate) + == self.tcx.crate_name(found_type.krate) + { + // A candidate was found of an item with the same name, from two separate + // versions of the same crate, let's clarify. + Some((self.tcx.def_span(candidate_def_id), self.tcx.def_span(found_type))) + } else { + None + } + }) { + let mut span: MultiSpan = vec![sp_candidate, sp_found].into(); + span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait"); + span.push_span_label(sp_candidate, "this type implements the required trait"); + span.push_span_label(sp_found, "this type doesn't implement the required trait"); + err.highlighted_span_note( + span, + vec![ + StringPart::normal( + "two types coming from two different versions of the same crate are \ + different types " + .to_string(), + ), + StringPart::highlighted("even if they look the same".to_string()), + ], + ); + } + err.help("you can use `cargo tree` to explore your dependency tree"); + return true; + } + if let [single] = &impl_candidates { + // If we have a single implementation, try to unify it with the trait ref + // that failed. This should uncover a better hint for what *is* implemented. if self.probe(|_| { let ocx = ObligationCtxt::new(self); @@ -1782,12 +1896,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let impl_candidates = impl_candidates .into_iter() .cloned() - .filter(|cand| { - !self.tcx.has_attrs_with_path( - cand.impl_def_id, - &[sym::diagnostic, sym::do_not_recommend], - ) - }) + .filter(|cand| !self.tcx.do_not_recommend_impl(cand.impl_def_id)) .collect::<Vec<_>>(); let def_id = trait_ref.def_id(); @@ -1799,43 +1908,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // Mentioning implementers of `Copy`, `Debug` and friends is not useful. return false; } - let mut impl_candidates: Vec<_> = self - .tcx - .all_impls(def_id) - // ignore `do_not_recommend` items - .filter(|def_id| { - !self - .tcx - .has_attrs_with_path(*def_id, &[sym::diagnostic, sym::do_not_recommend]) - }) - // Ignore automatically derived impls and `!Trait` impls. - .filter_map(|def_id| self.tcx.impl_trait_header(def_id)) - .filter_map(|header| { - (header.polarity != ty::ImplPolarity::Negative - || self.tcx.is_automatically_derived(def_id)) - .then(|| header.trait_ref.instantiate_identity()) - }) - .filter(|trait_ref| { - let self_ty = trait_ref.self_ty(); - // Avoid mentioning type parameters. - if let ty::Param(_) = self_ty.kind() { - false - } - // Avoid mentioning types that are private to another crate - else if let ty::Adt(def, _) = self_ty.peel_refs().kind() { - // FIXME(compiler-errors): This could be generalized, both to - // be more granular, and probably look past other `#[fundamental]` - // types, too. - self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx) - } else { - true - } - }) - .collect(); - - impl_candidates.sort_by_key(|tr| tr.to_string()); - impl_candidates.dedup(); - return report(impl_candidates, err); + return report(alternative_candidates(def_id), err); } // Sort impl candidates so that ordering is consistent for UI tests. diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs index 89ae6f4ccab..40a1c184009 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs @@ -19,11 +19,10 @@ use rustc_middle::ty::print::{with_no_trimmed_paths, PrintTraitRefExt as _}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::{ErrorGuaranteed, ExpnKind, Span}; +pub use self::overflow::*; use crate::error_reporting::TypeErrCtxt; use crate::traits::{FulfillmentError, FulfillmentErrorCode}; -pub use self::overflow::*; - // When outputting impl candidates, prefer showing those that are more similar. // // We also compare candidates after skipping lifetimes, which has a lower diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs index f65de590ccf..f656f9b0e38 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs @@ -1,29 +1,27 @@ -use super::{ObligationCauseCode, PredicateObligation}; -use crate::error_reporting::TypeErrCtxt; -use crate::errors::{ - EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented, -}; -use crate::infer::InferCtxtExt; -use rustc_ast::AttrArgs; -use rustc_ast::AttrArgsEq; -use rustc_ast::AttrKind; -use rustc_ast::{Attribute, MetaItem, NestedMetaItem}; -use rustc_attr as attr; +use std::iter; +use std::path::PathBuf; + +use rustc_ast::{AttrArgs, AttrArgsEq, AttrKind, Attribute, MetaItem, NestedMetaItem}; use rustc_data_structures::fx::FxHashMap; -use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed}; -use rustc_hir as hir; +use rustc_errors::codes::*; +use rustc_errors::{struct_span_code_err, ErrorGuaranteed}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_macros::LintDiagnostic; use rustc_middle::bug; use rustc_middle::ty::print::PrintTraitRefExt as _; -use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::{self, GenericParamDefKind, TyCtxt}; +use rustc_middle::ty::{self, GenericArgsRef, GenericParamDefKind, TyCtxt}; use rustc_parse_format::{ParseMode, Parser, Piece, Position}; use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; -use std::iter; -use std::path::PathBuf; +use {rustc_attr as attr, rustc_hir as hir}; + +use super::{ObligationCauseCode, PredicateObligation}; +use crate::error_reporting::TypeErrCtxt; +use crate::errors::{ + EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented, +}; +use crate::infer::InferCtxtExt; /// The symbols which are always allowed in a format string static ALLOWED_FORMAT_SYMBOLS: &[Symbol] = &[ @@ -75,10 +73,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { } }); - let impl_def_id_and_args = if self_match_impls.len() == 1 { - self_match_impls[0] - } else if fuzzy_match_impls.len() == 1 { - fuzzy_match_impls[0] + let impl_def_id_and_args = if let [impl_] = self_match_impls[..] { + impl_ + } else if let [impl_] = fuzzy_match_impls[..] { + impl_ } else { return None; }; diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index d1381d24764..9269177eb50 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -1,34 +1,33 @@ // ignore-tidy-filelength -use super::{ - DefIdOrName, FindExprBySpan, ImplCandidate, Obligation, ObligationCause, ObligationCauseCode, - PredicateObligation, -}; - -use crate::error_reporting::TypeErrCtxt; -use crate::errors; -use crate::traits::{ImplDerivedCause, NormalizeExt, ObligationCtxt}; +use std::assert_matches::debug_assert_matches; +use std::borrow::Cow; +use std::iter; +use itertools::{EitherOrBoth, Itertools}; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::stack::ensure_sufficient_stack; +use rustc_errors::codes::*; use rustc_errors::{ - codes::*, pluralize, struct_span_code_err, Applicability, Diag, EmissionGuarantee, MultiSpan, - Style, SuggestionStyle, + pluralize, struct_span_code_err, Applicability, Diag, EmissionGuarantee, MultiSpan, Style, + SuggestionStyle, }; use rustc_hir as hir; -use rustc_hir::def::CtorOf; -use rustc_hir::def::{DefKind, Res}; +use rustc_hir::def::{CtorOf, DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::Visitor; -use rustc_hir::is_range_literal; use rustc_hir::lang_items::LangItem; -use rustc_hir::{CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, HirId, Node}; -use rustc_infer::infer::InferCtxt; -use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk}; +use rustc_hir::{ + is_range_literal, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, HirId, Node, +}; +use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, InferOk}; use rustc_middle::hir::map; use rustc_middle::traits::IsConstable; use rustc_middle::ty::error::TypeError; -use rustc_middle::ty::print::PrintPolyTraitRefExt; +use rustc_middle::ty::print::{ + with_forced_trimmed_paths, with_no_trimmed_paths, PrintPolyTraitPredicateExt as _, + PrintPolyTraitRefExt, PrintTraitPredicateExt as _, +}; use rustc_middle::ty::{ self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, GenericArgs, InferTy, IsSuggestable, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable, TypeFolder, @@ -39,19 +38,16 @@ use rustc_span::def_id::LocalDefId; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP}; use rustc_target::spec::abi; -use std::assert_matches::debug_assert_matches; -use std::borrow::Cow; -use std::iter; +use super::{ + DefIdOrName, FindExprBySpan, ImplCandidate, Obligation, ObligationCause, ObligationCauseCode, + PredicateObligation, +}; +use crate::error_reporting::TypeErrCtxt; +use crate::errors; use crate::infer::InferCtxtExt as _; use crate::traits::query::evaluate_obligation::InferCtxtExt as _; -use rustc_middle::ty::print::{ - with_forced_trimmed_paths, with_no_trimmed_paths, PrintPolyTraitPredicateExt as _, - PrintTraitPredicateExt as _, -}; - -use itertools::EitherOrBoth; -use itertools::Itertools; +use crate::traits::{ImplDerivedCause, NormalizeExt, ObligationCtxt}; #[derive(Debug)] pub enum CoroutineInteriorOrUpvar { @@ -5304,7 +5300,8 @@ impl<'v> Visitor<'v> for FindTypeParam { match ty.kind { hir::TyKind::Ptr(_) | hir::TyKind::Ref(..) | hir::TyKind::TraitObject(..) => {} hir::TyKind::Path(hir::QPath::Resolved(None, path)) - if path.segments.len() == 1 && path.segments[0].ident.name == self.param => + if let [segment] = path.segments + && segment.ident.name == self.param => { if !self.nested { debug!(?ty, "FindTypeParam::visit_ty"); diff --git a/compiler/rustc_trait_selection/src/errors.rs b/compiler/rustc_trait_selection/src/errors.rs index 0ee4485a365..78f1f7d9b9b 100644 --- a/compiler/rustc_trait_selection/src/errors.rs +++ b/compiler/rustc_trait_selection/src/errors.rs @@ -1,19 +1,18 @@ +use std::path::PathBuf; + use rustc_data_structures::fx::FxHashSet; +use rustc_errors::codes::*; use rustc_errors::{ - codes::*, Applicability, Diag, DiagCtxtHandle, DiagMessage, DiagStyledString, Diagnostic, + Applicability, Diag, DiagCtxtHandle, DiagMessage, DiagStyledString, Diagnostic, EmissionGuarantee, IntoDiagArg, Level, MultiSpan, SubdiagMessageOp, Subdiagnostic, }; use rustc_hir as hir; use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::{walk_ty, Visitor}; -use rustc_hir::FnRetTy; -use rustc_hir::GenericParamKind; +use rustc_hir::{FnRetTy, GenericParamKind}; use rustc_macros::{Diagnostic, Subdiagnostic}; -use rustc_middle::ty::print::TraitRefPrintOnlyTraitPath; -use rustc_middle::ty::{ - self, print::PrintTraitRefExt as _, Binder, ClosureKind, FnSig, PolyTraitRef, Region, Ty, - TyCtxt, -}; +use rustc_middle::ty::print::{PrintTraitRefExt as _, TraitRefPrintOnlyTraitPath}; +use rustc_middle::ty::{self, Binder, ClosureKind, FnSig, PolyTraitRef, Region, Ty, TyCtxt}; use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::{BytePos, Span}; @@ -22,8 +21,6 @@ use crate::error_reporting::infer::nice_region_error::placeholder_error::Highlig use crate::error_reporting::infer::ObligationCauseAsDiagArg; use crate::fluent_generated as fluent; -use std::path::PathBuf; - pub mod note_and_explain; #[derive(Diagnostic)] @@ -1585,10 +1582,7 @@ pub enum TypeErrorAdditionalDiags { span: Span, code: String, }, - #[multipart_suggestion( - trait_selection_meant_str_literal, - applicability = "machine-applicable" - )] + #[multipart_suggestion(trait_selection_meant_str_literal, applicability = "machine-applicable")] MeantStrLiteral { #[suggestion_part(code = "\"")] start: Span, diff --git a/compiler/rustc_trait_selection/src/errors/note_and_explain.rs b/compiler/rustc_trait_selection/src/errors/note_and_explain.rs index 1f18cd8c8d8..b1477763028 100644 --- a/compiler/rustc_trait_selection/src/errors/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/errors/note_and_explain.rs @@ -1,10 +1,12 @@ -use crate::error_reporting::infer::nice_region_error::find_anon_type; -use crate::fluent_generated as fluent; use rustc_errors::{Diag, EmissionGuarantee, IntoDiagArg, SubdiagMessageOp, Subdiagnostic}; use rustc_hir::def_id::LocalDefId; use rustc_middle::bug; use rustc_middle::ty::{self, TyCtxt}; -use rustc_span::{symbol::kw, Span}; +use rustc_span::symbol::kw; +use rustc_span::Span; + +use crate::error_reporting::infer::nice_region_error::find_anon_type; +use crate::fluent_generated as fluent; struct DescriptionCtx<'a> { span: Option<Span>, diff --git a/compiler/rustc_trait_selection/src/infer.rs b/compiler/rustc_trait_selection/src/infer.rs index ad087620ae0..c22925b73e3 100644 --- a/compiler/rustc_trait_selection/src/infer.rs +++ b/compiler/rustc_trait_selection/src/infer.rs @@ -1,20 +1,18 @@ -use crate::infer::at::ToTrace; -use crate::traits::query::evaluate_obligation::InferCtxtExt as _; -use crate::traits::{self, Obligation, ObligationCause, ObligationCtxt, SelectionContext}; +use std::fmt::Debug; use rustc_hir::def_id::DefId; use rustc_hir::lang_items::LangItem; +pub use rustc_infer::infer::*; use rustc_macros::extension; use rustc_middle::arena::ArenaAllocatable; use rustc_middle::infer::canonical::{Canonical, CanonicalQueryResponse, QueryResponse}; use rustc_middle::traits::query::NoSolution; -use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitableExt}; -use rustc_middle::ty::{GenericArg, Upcast}; +use rustc_middle::ty::{self, GenericArg, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, Upcast}; use rustc_span::DUMMY_SP; -use std::fmt::Debug; - -pub use rustc_infer::infer::*; +use crate::infer::at::ToTrace; +use crate::traits::query::evaluate_obligation::InferCtxtExt as _; +use crate::traits::{self, Obligation, ObligationCause, ObligationCtxt, SelectionContext}; #[extension(pub trait InferCtxtExt<'tcx>)] impl<'tcx> InferCtxt<'tcx> { diff --git a/compiler/rustc_trait_selection/src/regions.rs b/compiler/rustc_trait_selection/src/regions.rs index 5f986e22f51..65762cfcd2e 100644 --- a/compiler/rustc_trait_selection/src/regions.rs +++ b/compiler/rustc_trait_selection/src/regions.rs @@ -1,10 +1,11 @@ -use crate::traits::ScrubbedTraitError; use rustc_infer::infer::outlives::env::OutlivesEnvironment; use rustc_infer::infer::{InferCtxt, RegionResolutionError}; use rustc_macros::extension; use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::ObligationCause; +use crate::traits::ScrubbedTraitError; + #[extension(pub trait InferCtxtRegionExt<'tcx>)] impl<'tcx> InferCtxt<'tcx> { /// Resolve regions, using the deep normalizer to normalize any type-outlives diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs index 76b88aeb0f7..de8951ef720 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -13,13 +13,11 @@ use rustc_middle::bug; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::{self, TyCtxt}; use rustc_next_trait_solver::solve::{GenerateProofTree, SolverDelegateEvalExt as _}; -use rustc_span::symbol::sym; - -use crate::traits::{FulfillmentError, FulfillmentErrorCode, ScrubbedTraitError}; use super::delegate::SolverDelegate; use super::inspect::{self, ProofTreeInferCtxtExt, ProofTreeVisitor}; use super::Certainty; +use crate::traits::{FulfillmentError, FulfillmentErrorCode, ScrubbedTraitError}; /// A trait engine using the new trait solver. /// @@ -441,10 +439,7 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> { source: CandidateSource::Impl(impl_def_id), result: _, } = candidate.kind() - && goal - .infcx() - .tcx - .has_attrs_with_path(impl_def_id, &[sym::diagnostic, sym::do_not_recommend]) + && goal.infcx().tcx.do_not_recommend_impl(impl_def_id) { return ControlFlow::Break(self.obligation.clone()); } diff --git a/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs b/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs index e8de8457440..1a459aa484f 100644 --- a/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs +++ b/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs @@ -9,6 +9,8 @@ //! coherence right now and was annoying to implement, so I am leaving it //! as is until we start using it for something else. +use std::assert_matches::assert_matches; + use rustc_ast_ir::try_visit; use rustc_ast_ir::visit::VisitorResult; use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk}; @@ -273,10 +275,10 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> { steps.push(step) } inspect::ProbeStep::MakeCanonicalResponse { shallow_certainty: c } => { - assert!(matches!( + assert_matches!( shallow_certainty.replace(c), None | Some(Certainty::Maybe(MaybeCause::Ambiguity)) - )); + ); } inspect::ProbeStep::NestedProbe(ref probe) => { match probe.kind { diff --git a/compiler/rustc_trait_selection/src/solve/normalize.rs b/compiler/rustc_trait_selection/src/solve/normalize.rs index ddaef7c159f..c93c40b4826 100644 --- a/compiler/rustc_trait_selection/src/solve/normalize.rs +++ b/compiler/rustc_trait_selection/src/solve/normalize.rs @@ -1,20 +1,22 @@ +use std::assert_matches::assert_matches; use std::fmt::Debug; use std::marker::PhantomData; -use crate::error_reporting::traits::OverflowCause; -use crate::error_reporting::InferCtxtErrorExt; -use crate::traits::query::evaluate_obligation::InferCtxtExt; -use crate::traits::{BoundVarReplacer, PlaceholderReplacer, ScrubbedTraitError}; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_infer::infer::at::At; use rustc_infer::infer::InferCtxt; use rustc_infer::traits::{FromSolverError, Obligation, TraitEngine}; use rustc_middle::traits::ObligationCause; -use rustc_middle::ty::{self, Ty, TyCtxt, UniverseIndex}; -use rustc_middle::ty::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable}; -use rustc_middle::ty::{TypeFoldable, TypeVisitableExt}; +use rustc_middle::ty::{ + self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, + TypeVisitableExt, UniverseIndex, +}; use super::{FulfillmentCtxt, NextSolverError}; +use crate::error_reporting::traits::OverflowCause; +use crate::error_reporting::InferCtxtErrorExt; +use crate::traits::query::evaluate_obligation::InferCtxtExt; +use crate::traits::{BoundVarReplacer, PlaceholderReplacer, ScrubbedTraitError}; /// Deeply normalize all aliases in `value`. This does not handle inference and expects /// its input to be already fully resolved. @@ -62,7 +64,7 @@ where E: FromSolverError<'tcx, NextSolverError<'tcx>>, { fn normalize_alias_ty(&mut self, alias_ty: Ty<'tcx>) -> Result<Ty<'tcx>, Vec<E>> { - assert!(matches!(alias_ty.kind(), ty::Alias(..))); + assert_matches!(alias_ty.kind(), ty::Alias(..)); let infcx = self.at.infcx; let tcx = infcx.tcx; diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 796f7fd5a54..29f78f9d5f0 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -1,11 +1,8 @@ //! Support code for rustdoc and external tools. //! You really don't want to be using this unless you need to. -use super::*; - -use crate::errors::UnableToConstructConstantValue; -use crate::infer::region_constraints::{Constraint, RegionConstraintData}; -use crate::traits::project::ProjectAndUnifyResult; +use std::collections::VecDeque; +use std::iter; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry}; use rustc_data_structures::unord::UnordSet; @@ -13,8 +10,10 @@ use rustc_infer::infer::DefineOpaqueTypes; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::{Region, RegionVid}; -use std::collections::VecDeque; -use std::iter; +use super::*; +use crate::errors::UnableToConstructConstantValue; +use crate::infer::region_constraints::{Constraint, RegionConstraintData}; +use crate::traits::project::ProjectAndUnifyResult; // FIXME(twk): this is obviously not nice to duplicate like that #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)] diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 7e996c5c5ef..2d843d8f174 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -4,15 +4,8 @@ //! [trait-resolution]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html //! [trait-specialization]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html -use crate::infer::outlives::env::OutlivesEnvironment; -use crate::infer::InferOk; -use crate::solve::inspect::{InspectGoal, ProofTreeInferCtxtExt, ProofTreeVisitor}; -use crate::solve::{deeply_normalize_for_diagnostics, inspect}; -use crate::traits::select::IntercrateAmbiguityCause; -use crate::traits::NormalizeExt; -use crate::traits::SkipLeakCheck; -use crate::traits::{util, FulfillmentErrorCode}; -use crate::traits::{Obligation, ObligationCause, PredicateObligation, SelectionContext}; +use std::fmt::Debug; + use rustc_data_structures::fx::FxIndexSet; use rustc_errors::{Diag, EmissionGuarantee}; use rustc_hir::def::DefKind; @@ -28,10 +21,18 @@ use rustc_middle::ty::{self, Ty, TyCtxt}; pub use rustc_next_trait_solver::coherence::*; use rustc_span::symbol::sym; use rustc_span::{Span, DUMMY_SP}; -use std::fmt::Debug; use super::ObligationCtxt; use crate::error_reporting::traits::suggest_new_overflow_limit; +use crate::infer::outlives::env::OutlivesEnvironment; +use crate::infer::InferOk; +use crate::solve::inspect::{InspectGoal, ProofTreeInferCtxtExt, ProofTreeVisitor}; +use crate::solve::{deeply_normalize_for_diagnostics, inspect}; +use crate::traits::select::IntercrateAmbiguityCause; +use crate::traits::{ + util, FulfillmentErrorCode, NormalizeExt, Obligation, ObligationCause, PredicateObligation, + SelectionContext, SkipLeakCheck, +}; pub struct OverlapResult<'tcx> { pub impl_header: ty::ImplHeader<'tcx>, diff --git a/compiler/rustc_trait_selection/src/traits/engine.rs b/compiler/rustc_trait_selection/src/traits/engine.rs index 49730b532a3..de1d4ef15ac 100644 --- a/compiler/rustc_trait_selection/src/traits/engine.rs +++ b/compiler/rustc_trait_selection/src/traits/engine.rs @@ -1,16 +1,6 @@ use std::cell::RefCell; use std::fmt::Debug; -use super::{FromSolverError, TraitEngine}; -use super::{FulfillmentContext, ScrubbedTraitError}; -use crate::error_reporting::InferCtxtErrorExt; -use crate::regions::InferCtxtRegionExt; -use crate::solve::FulfillmentCtxt as NextFulfillmentCtxt; -use crate::solve::NextSolverError; -use crate::traits::fulfill::OldSolverError; -use crate::traits::NormalizeExt; -use crate::traits::StructurallyNormalizeExt; -use crate::traits::{FulfillmentError, Obligation, ObligationCause, PredicateObligation}; use rustc_data_structures::fx::FxIndexSet; use rustc_errors::ErrorGuaranteed; use rustc_hir::def_id::{DefId, LocalDefId}; @@ -19,16 +9,22 @@ use rustc_infer::infer::canonical::{ Canonical, CanonicalQueryResponse, CanonicalVarValues, QueryResponse, }; use rustc_infer::infer::outlives::env::OutlivesEnvironment; -use rustc_infer::infer::RegionResolutionError; -use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk}; +use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk, RegionResolutionError}; use rustc_macros::extension; use rustc_middle::arena::ArenaAllocatable; use rustc_middle::traits::query::NoSolution; use rustc_middle::ty::error::TypeError; -use rustc_middle::ty::TypeFoldable; -use rustc_middle::ty::Upcast; -use rustc_middle::ty::Variance; -use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, Upcast, Variance}; + +use super::{FromSolverError, FulfillmentContext, ScrubbedTraitError, TraitEngine}; +use crate::error_reporting::InferCtxtErrorExt; +use crate::regions::InferCtxtRegionExt; +use crate::solve::{FulfillmentCtxt as NextFulfillmentCtxt, NextSolverError}; +use crate::traits::fulfill::OldSolverError; +use crate::traits::{ + FulfillmentError, NormalizeExt, Obligation, ObligationCause, PredicateObligation, + StructurallyNormalizeExt, +}; #[extension(pub trait TraitEngineExt<'tcx, E>)] impl<'tcx, E> dyn TraitEngine<'tcx, E> diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index cc0bb7a60b2..a6db22ec15a 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -1,32 +1,29 @@ -use crate::infer::{InferCtxt, TyOrConstInferVar}; -use crate::traits::normalize::normalize_with_depth_to; +use std::marker::PhantomData; + use rustc_data_structures::captures::Captures; -use rustc_data_structures::obligation_forest::ProcessResult; -use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome}; -use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor}; +use rustc_data_structures::obligation_forest::{ + Error, ForestObligation, ObligationForest, ObligationProcessor, Outcome, ProcessResult, +}; use rustc_infer::infer::DefineOpaqueTypes; -use rustc_infer::traits::{FromSolverError, ProjectionCacheKey}; -use rustc_infer::traits::{PolyTraitObligation, SelectionError, TraitEngine}; +use rustc_infer::traits::{ + FromSolverError, PolyTraitObligation, ProjectionCacheKey, SelectionError, TraitEngine, +}; use rustc_middle::bug; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::abstract_const::NotConstEvaluatable; use rustc_middle::ty::error::{ExpectedFound, TypeError}; -use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::{self, Binder, Const, TypeVisitableExt}; -use std::marker::PhantomData; +use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt}; use super::project::{self, ProjectAndUnifyResult}; use super::select::SelectionContext; -use super::wf; -use super::EvaluationResult; -use super::PredicateObligation; -use super::Unimplemented; -use super::{const_evaluatable, ScrubbedTraitError}; -use super::{FulfillmentError, FulfillmentErrorCode}; - +use super::{ + const_evaluatable, wf, EvaluationResult, FulfillmentError, FulfillmentErrorCode, + PredicateObligation, ScrubbedTraitError, Unimplemented, +}; use crate::error_reporting::InferCtxtErrorExt; -use crate::traits::project::PolyProjectionObligation; -use crate::traits::project::ProjectionCacheKeyExt as _; +use crate::infer::{InferCtxt, TyOrConstInferVar}; +use crate::traits::normalize::normalize_with_depth_to; +use crate::traits::project::{PolyProjectionObligation, ProjectionCacheKeyExt as _}; use crate::traits::query::evaluate_obligation::InferCtxtExt; impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> { diff --git a/compiler/rustc_trait_selection/src/traits/misc.rs b/compiler/rustc_trait_selection/src/traits/misc.rs index d749b686803..3e65194577e 100644 --- a/compiler/rustc_trait_selection/src/traits/misc.rs +++ b/compiler/rustc_trait_selection/src/traits/misc.rs @@ -1,7 +1,6 @@ //! Miscellaneous type-system utilities that are too small to deserve their own modules. -use crate::regions::InferCtxtRegionExt; -use crate::traits::{self, FulfillmentError, ObligationCause}; +use std::assert_matches::assert_matches; use hir::LangItem; use rustc_ast::Mutability; @@ -12,6 +11,8 @@ use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt}; use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt, TypeVisitableExt}; use super::outlives_bounds::InferCtxtExt; +use crate::regions::InferCtxtRegionExt; +use crate::traits::{self, FulfillmentError, ObligationCause}; pub enum CopyImplementationError<'tcx> { InfringingFields(Vec<(&'tcx ty::FieldDef, Ty<'tcx>, InfringingFieldsReason<'tcx>)>), @@ -93,7 +94,7 @@ pub fn type_allowed_to_implement_const_param_ty<'tcx>( lang_item: LangItem, parent_cause: ObligationCause<'tcx>, ) -> Result<(), ConstParamTyImplementationError<'tcx>> { - assert!(matches!(lang_item, LangItem::ConstParamTy | LangItem::UnsizedConstParamTy)); + assert_matches!(lang_item, LangItem::ConstParamTy | LangItem::UnsizedConstParamTy); let inner_tys: Vec<_> = match *self_type.kind() { // Trivially okay as these types are all: diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index c57ca014799..a350b76a704 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -22,51 +22,55 @@ mod util; pub mod vtable; pub mod wf; -use crate::error_reporting::InferCtxtErrorExt; -use crate::infer::outlives::env::OutlivesEnvironment; -use crate::infer::{InferCtxt, TyCtxtInferExt}; -use crate::regions::InferCtxtRegionExt; -use crate::traits::query::evaluate_obligation::InferCtxtExt as _; +use std::fmt::Debug; +use std::ops::ControlFlow; + use rustc_errors::ErrorGuaranteed; +pub use rustc_infer::traits::*; use rustc_middle::query::Providers; use rustc_middle::span_bug; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt}; -use rustc_middle::ty::{self, Ty, TyCtxt, TypeFolder, TypeSuperVisitable, Upcast}; -use rustc_middle::ty::{GenericArgs, GenericArgsRef}; +use rustc_middle::ty::{ + self, GenericArgs, GenericArgsRef, Ty, TyCtxt, TypeFolder, TypeSuperVisitable, Upcast, +}; use rustc_span::def_id::DefId; use rustc_span::Span; -use std::fmt::Debug; -use std::ops::ControlFlow; - -pub use self::coherence::{add_placeholder_note, orphan_check_trait_ref, overlapping_impls}; -pub use self::coherence::{InCrate, IsFirstInputType, UncoveredTyParams}; -pub use self::coherence::{OrphanCheckErr, OrphanCheckMode, OverlapResult}; +pub use self::coherence::{ + add_placeholder_note, orphan_check_trait_ref, overlapping_impls, InCrate, IsFirstInputType, + OrphanCheckErr, OrphanCheckMode, OverlapResult, UncoveredTyParams, +}; pub use self::engine::{ObligationCtxt, TraitEngineExt}; pub use self::fulfill::{FulfillmentContext, OldSolverError, PendingPredicateObligation}; pub use self::normalize::NormalizeExt; -pub use self::object_safety::hir_ty_lowering_object_safety_violations; -pub use self::object_safety::is_vtable_safe_method; -pub use self::object_safety::object_safety_violations_for_assoc_item; -pub use self::object_safety::ObjectSafetyViolation; +pub use self::object_safety::{ + hir_ty_lowering_object_safety_violations, is_vtable_safe_method, + object_safety_violations_for_assoc_item, ObjectSafetyViolation, +}; pub use self::project::{normalize_inherent_projection, normalize_projection_ty}; -pub use self::select::{EvaluationCache, SelectionCache, SelectionContext}; -pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError}; -pub use self::specialize::specialization_graph::FutureCompatOverlapError; -pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind; +pub use self::select::{ + EvaluationCache, EvaluationResult, IntercrateAmbiguityCause, OverflowError, SelectionCache, + SelectionContext, +}; +pub use self::specialize::specialization_graph::{ + FutureCompatOverlapError, FutureCompatOverlapErrorKind, +}; pub use self::specialize::{ specialization_graph, translate_args, translate_args_with_cause, OverlapError, }; pub use self::structural_normalize::StructurallyNormalizeExt; -pub use self::util::elaborate; -pub use self::util::{expand_trait_aliases, TraitAliasExpander, TraitAliasExpansionInfo}; -pub use self::util::{impl_item_is_final, upcast_choices}; -pub use self::util::{supertraits, transitive_bounds, transitive_bounds_that_define_assoc_item}; -pub use self::util::{with_replaced_escaping_bound_vars, BoundVarReplacer, PlaceholderReplacer}; - -pub use rustc_infer::traits::*; +pub use self::util::{ + elaborate, expand_trait_aliases, impl_item_is_final, supertraits, + transitive_bounds_that_define_assoc_item, upcast_choices, with_replaced_escaping_bound_vars, + BoundVarReplacer, PlaceholderReplacer, TraitAliasExpander, TraitAliasExpansionInfo, +}; +use crate::error_reporting::InferCtxtErrorExt; +use crate::infer::outlives::env::OutlivesEnvironment; +use crate::infer::{InferCtxt, TyCtxtInferExt}; +use crate::regions::InferCtxtRegionExt; +use crate::traits::query::evaluate_obligation::InferCtxtExt as _; pub struct FulfillmentError<'tcx> { pub obligation: PredicateObligation<'tcx>, diff --git a/compiler/rustc_trait_selection/src/traits/normalize.rs b/compiler/rustc_trait_selection/src/traits/normalize.rs index 26cb9bb5a3d..81f8633ba95 100644 --- a/compiler/rustc_trait_selection/src/traits/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/normalize.rs @@ -1,20 +1,24 @@ //! Deeply normalize types using the old trait solver. -use super::SelectionContext; -use super::{project, with_replaced_escaping_bound_vars, BoundVarReplacer, PlaceholderReplacer}; -use crate::error_reporting::traits::OverflowCause; -use crate::error_reporting::InferCtxtErrorExt; -use crate::solve::NextSolverError; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_infer::infer::at::At; use rustc_infer::infer::InferOk; -use rustc_infer::traits::FromSolverError; -use rustc_infer::traits::PredicateObligation; -use rustc_infer::traits::{Normalized, Obligation, TraitEngine}; +use rustc_infer::traits::{ + FromSolverError, Normalized, Obligation, PredicateObligation, TraitEngine, +}; use rustc_macros::extension; use rustc_middle::traits::{ObligationCause, ObligationCauseCode, Reveal}; -use rustc_middle::ty::{self, Ty, TyCtxt, TypeFolder}; -use rustc_middle::ty::{TypeFoldable, TypeSuperFoldable, TypeVisitable, TypeVisitableExt}; +use rustc_middle::ty::{ + self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt, +}; + +use super::{ + project, with_replaced_escaping_bound_vars, BoundVarReplacer, PlaceholderReplacer, + SelectionContext, +}; +use crate::error_reporting::traits::OverflowCause; +use crate::error_reporting::InferCtxtErrorExt; +use crate::solve::NextSolverError; #[extension(pub trait NormalizeExt<'tcx>)] impl<'tcx> At<'_, 'tcx> { diff --git a/compiler/rustc_trait_selection/src/traits/object_safety.rs b/compiler/rustc_trait_selection/src/traits/object_safety.rs index ec19cf27668..8e1fc0d7fe6 100644 --- a/compiler/rustc_trait_selection/src/traits/object_safety.rs +++ b/compiler/rustc_trait_selection/src/traits/object_safety.rs @@ -8,11 +8,9 @@ //! - not reference the erased type `Self` except for in this receiver; //! - not have generic type parameters. -use super::elaborate; +use std::iter; +use std::ops::ControlFlow; -use crate::infer::TyCtxtInferExt; -use crate::traits::query::evaluate_obligation::InferCtxtExt; -use crate::traits::{util, Obligation, ObligationCause}; use rustc_errors::FatalError; use rustc_hir as hir; use rustc_hir::def_id::DefId; @@ -27,9 +25,10 @@ use rustc_span::Span; use rustc_target::abi::Abi; use smallvec::SmallVec; -use std::iter; -use std::ops::ControlFlow; - +use super::elaborate; +use crate::infer::TyCtxtInferExt; +use crate::traits::query::evaluate_obligation::InferCtxtExt; +use crate::traits::{util, Obligation, ObligationCause}; pub use crate::traits::{MethodViolationCode, ObjectSafetyViolation}; /// Returns the object safety violations that affect HIR ty lowering. diff --git a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs index 1dc2ebfaa7a..0fe75059904 100644 --- a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs +++ b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs @@ -1,15 +1,15 @@ -use crate::infer::InferCtxt; -use crate::traits::{ObligationCause, ObligationCtxt}; use rustc_data_structures::fx::FxIndexSet; use rustc_infer::infer::resolve::OpportunisticRegionResolver; use rustc_infer::infer::InferOk; use rustc_macros::extension; use rustc_middle::infer::canonical::{OriginalQueryValues, QueryRegionConstraints}; use rustc_middle::span_bug; +pub use rustc_middle::traits::query::OutlivesBound; use rustc_middle::ty::{self, ParamEnv, Ty, TypeFolder, TypeVisitableExt}; use rustc_span::def_id::LocalDefId; -pub use rustc_middle::traits::query::OutlivesBound; +use crate::infer::InferCtxt; +use crate::traits::{ObligationCause, ObligationCtxt}; pub type BoundsCompat<'a, 'tcx: 'a> = impl Iterator<Item = OutlivesBound<'tcx>> + 'a; pub type Bounds<'a, 'tcx: 'a> = impl Iterator<Item = OutlivesBound<'tcx>> + 'a; diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index b96e0c8a977..8a17d7ed641 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -2,29 +2,6 @@ use std::ops::ControlFlow; -use super::specialization_graph; -use super::translate_args; -use super::util; -use super::MismatchedProjectionTypes; -use super::Obligation; -use super::ObligationCause; -use super::PredicateObligation; -use super::Selection; -use super::SelectionContext; -use super::SelectionError; -use super::{Normalized, NormalizedTerm, ProjectionCacheEntry, ProjectionCacheKey}; -use rustc_infer::traits::ObligationCauseCode; -use rustc_middle::traits::BuiltinImplSource; -use rustc_middle::traits::ImplSource; -use rustc_middle::traits::ImplSourceUserDefinedData; -use rustc_middle::{bug, span_bug}; - -use crate::errors::InherentProjectionNormalizationOverflow; -use crate::infer::{BoundRegionConversionTime, InferOk}; -use crate::traits::normalize::normalize_with_depth; -use crate::traits::normalize::normalize_with_depth_to; -use crate::traits::query::evaluate_obligation::InferCtxtExt as _; -use crate::traits::select::ProjectionMatchesProjection; use rustc_data_structures::sso::SsoHashSet; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_errors::ErrorGuaranteed; @@ -32,13 +9,26 @@ use rustc_hir::def::DefKind; use rustc_hir::lang_items::LangItem; use rustc_infer::infer::resolve::OpportunisticRegionResolver; use rustc_infer::infer::DefineOpaqueTypes; +use rustc_infer::traits::ObligationCauseCode; use rustc_middle::traits::select::OverflowError; +pub use rustc_middle::traits::Reveal; +use rustc_middle::traits::{BuiltinImplSource, ImplSource, ImplSourceUserDefinedData}; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::visit::{MaxUniverse, TypeVisitable, TypeVisitableExt}; use rustc_middle::ty::{self, Term, Ty, TyCtxt, Upcast}; +use rustc_middle::{bug, span_bug}; use rustc_span::symbol::sym; -pub use rustc_middle::traits::Reveal; +use super::{ + specialization_graph, translate_args, util, MismatchedProjectionTypes, Normalized, + NormalizedTerm, Obligation, ObligationCause, PredicateObligation, ProjectionCacheEntry, + ProjectionCacheKey, Selection, SelectionContext, SelectionError, +}; +use crate::errors::InherentProjectionNormalizationOverflow; +use crate::infer::{BoundRegionConversionTime, InferOk}; +use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to}; +use crate::traits::query::evaluate_obligation::InferCtxtExt as _; +use crate::traits::select::ProjectionMatchesProjection; pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>; @@ -1120,7 +1110,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( | ty::Error(_) => false, } } else if tcx.is_lang_item(trait_ref.def_id, LangItem::PointeeTrait) { - let tail = selcx.tcx().struct_tail_with_normalize( + let tail = selcx.tcx().struct_tail_raw( self_ty, |ty| { // We throw away any obligations we get from this, since we normalize @@ -1159,10 +1149,10 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( | ty::Never // Extern types have unit metadata, according to RFC 2850 | ty::Foreign(_) - // If returned by `struct_tail_without_normalization` this is a unit struct + // If returned by `struct_tail` this is a unit struct // without any fields, or not a struct, and therefore is Sized. | ty::Adt(..) - // If returned by `struct_tail_without_normalization` this is the empty tuple. + // If returned by `struct_tail` this is the empty tuple. | ty::Tuple(..) // Integers and floats are always Sized, and so have unit type metadata. | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true, diff --git a/compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs b/compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs index 7dc051e6fe9..d3a1ed52d2e 100644 --- a/compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs +++ b/compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs @@ -1,12 +1,12 @@ -use crate::traits::query::normalize::QueryNormalizeExt; -use crate::traits::query::NoSolution; -use crate::traits::{Normalized, ObligationCause, ObligationCtxt}; - use rustc_data_structures::fx::FxHashSet; use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult}; use rustc_middle::ty::{self, EarlyBinder, ParamEnvAnd, Ty, TyCtxt}; use rustc_span::{Span, DUMMY_SP}; +use crate::traits::query::normalize::QueryNormalizeExt; +use crate::traits::query::NoSolution; +use crate::traits::{Normalized, ObligationCause, ObligationCtxt}; + /// This returns true if the type `ty` is "trivial" for /// dropck-outlives -- that is, if it doesn't require any types to /// outlive. This is similar but not *quite* the same as the @@ -42,8 +42,15 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { | ty::Foreign(..) | ty::Error(_) => true, - // `T is PAT`, `[T; N]`, and `[T]` have same properties as T. - ty::Pat(ty, _) | ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, *ty), + // `T is PAT` and `[T]` have same properties as T. + ty::Pat(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, *ty), + ty::Array(ty, size) => { + // Empty array never has a dtor. See issue #110288. + match size.try_to_target_usize(tcx) { + Some(0) => true, + _ => trivial_dropck_outlives(tcx, *ty), + } + } // (T1..Tn) and closures have same properties as T1..Tn -- // check if *all* of them are trivial. diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index 75f1af7fcf5..247b6e4823c 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -2,26 +2,26 @@ //! which folds deeply, invoking the underlying //! `normalize_canonicalized_projection_ty` query when it encounters projections. -use crate::error_reporting::traits::OverflowCause; -use crate::error_reporting::InferCtxtErrorExt; -use crate::infer::at::At; -use crate::infer::canonical::OriginalQueryValues; -use crate::infer::{InferCtxt, InferOk}; -use crate::traits::normalize::needs_normalization; -use crate::traits::Normalized; -use crate::traits::{BoundVarReplacer, PlaceholderReplacer, ScrubbedTraitError}; -use crate::traits::{ObligationCause, PredicateObligation, Reveal}; use rustc_data_structures::sso::SsoHashMap; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_macros::extension; +pub use rustc_middle::traits::query::NormalizationResult; use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable}; use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor}; use rustc_span::DUMMY_SP; use super::NoSolution; - -pub use rustc_middle::traits::query::NormalizationResult; +use crate::error_reporting::traits::OverflowCause; +use crate::error_reporting::InferCtxtErrorExt; +use crate::infer::at::At; +use crate::infer::canonical::OriginalQueryValues; +use crate::infer::{InferCtxt, InferOk}; +use crate::traits::normalize::needs_normalization; +use crate::traits::{ + BoundVarReplacer, Normalized, ObligationCause, PlaceholderReplacer, PredicateObligation, + Reveal, ScrubbedTraitError, +}; #[extension(pub trait QueryNormalizeExt<'tcx>)] impl<'cx, 'tcx> At<'cx, 'tcx> { diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs index aca16950223..5e4de43d04f 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs @@ -1,14 +1,14 @@ -use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; -use crate::traits::ObligationCtxt; use rustc_hir::def_id::{DefId, CRATE_DEF_ID}; use rustc_infer::traits::Obligation; +pub use rustc_middle::traits::query::type_op::AscribeUserType; use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::{ObligationCause, ObligationCauseCode}; use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt, UserArgs, UserSelfTy, UserType}; - -pub use rustc_middle::traits::query::type_op::AscribeUserType; use rustc_span::{Span, DUMMY_SP}; +use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; +use crate::traits::ObligationCtxt; + impl<'tcx> super::QueryTypeOp<'tcx> for AscribeUserType<'tcx> { type QueryResponse = (); diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/custom.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/custom.rs index d533e69a4fa..34e678e93d1 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/custom.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/custom.rs @@ -1,14 +1,15 @@ -use crate::infer::canonical::query_response; -use crate::infer::InferCtxt; -use crate::traits::query::type_op::TypeOpOutput; -use crate::traits::ObligationCtxt; +use std::fmt; + use rustc_errors::ErrorGuaranteed; use rustc_infer::infer::region_constraints::RegionConstraintData; use rustc_middle::traits::query::NoSolution; use rustc_middle::ty::{TyCtxt, TypeFoldable}; use rustc_span::Span; -use std::fmt; +use crate::infer::canonical::query_response; +use crate::infer::InferCtxt; +use crate::traits::query::type_op::TypeOpOutput; +use crate::traits::ObligationCtxt; pub struct CustomTypeOp<F> { closure: F, diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/eq.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/eq.rs index 57e649f3e43..656130cda19 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/eq.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/eq.rs @@ -1,10 +1,10 @@ -use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; -use crate::traits::ObligationCtxt; +pub use rustc_middle::traits::query::type_op::Eq; use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::ObligationCause; use rustc_middle::ty::{ParamEnvAnd, TyCtxt}; -pub use rustc_middle::traits::query::type_op::Eq; +use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; +use crate::traits::ObligationCtxt; impl<'tcx> super::QueryTypeOp<'tcx> for Eq<'tcx> { type QueryResponse = (); diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs index 8525215a3bc..b5b209c1af7 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs @@ -1,7 +1,3 @@ -use crate::traits::query::NoSolution; -use crate::traits::wf; -use crate::traits::ObligationCtxt; - use rustc_infer::infer::canonical::Canonical; use rustc_infer::infer::resolve::OpportunisticRegionResolver; use rustc_infer::traits::query::OutlivesBound; @@ -14,6 +10,9 @@ use rustc_span::DUMMY_SP; use rustc_type_ir::outlives::{push_outlives_components, Component}; use smallvec::{smallvec, SmallVec}; +use crate::traits::query::NoSolution; +use crate::traits::{wf, ObligationCtxt}; + #[derive(Copy, Clone, Debug, HashStable, TypeFoldable, TypeVisitable)] pub struct ImpliedOutlivesBounds<'tcx> { pub ty: Ty<'tcx>, diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs index c1b1bfd300b..2f64ed963f9 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs @@ -1,8 +1,5 @@ -use crate::infer::canonical::{ - Canonical, CanonicalQueryResponse, OriginalQueryValues, QueryRegionConstraints, -}; -use crate::infer::{InferCtxt, InferOk}; -use crate::traits::{ObligationCause, ObligationCtxt}; +use std::fmt; + use rustc_errors::ErrorGuaranteed; use rustc_infer::infer::canonical::Certainty; use rustc_infer::traits::PredicateObligation; @@ -10,7 +7,12 @@ use rustc_middle::traits::query::NoSolution; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::{ParamEnvAnd, TyCtxt}; use rustc_span::Span; -use std::fmt; + +use crate::infer::canonical::{ + Canonical, CanonicalQueryResponse, OriginalQueryValues, QueryRegionConstraints, +}; +use crate::infer::{InferCtxt, InferOk}; +use crate::traits::{ObligationCause, ObligationCtxt}; pub mod ascribe_user_type; pub mod custom; diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs index e9948bf1f71..41c34f6da29 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs @@ -1,12 +1,13 @@ -use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; -use crate::traits::ObligationCtxt; +use std::fmt; + +pub use rustc_middle::traits::query::type_op::Normalize; use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::ObligationCause; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::{self, Lift, ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt}; -use std::fmt; -pub use rustc_middle::traits::query::type_op::Normalize; +use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; +use crate::traits::ObligationCtxt; impl<'tcx, T> super::QueryTypeOp<'tcx> for Normalize<T> where diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/outlives.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/outlives.rs index 3e7aa52dcfe..49d324fa62e 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/outlives.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/outlives.rs @@ -1,11 +1,12 @@ +use rustc_macros::{HashStable, TypeFoldable, TypeVisitable}; +use rustc_middle::traits::query::{DropckOutlivesResult, NoSolution}; +use rustc_middle::ty::{ParamEnvAnd, Ty, TyCtxt}; + use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; use crate::traits::query::dropck_outlives::{ compute_dropck_outlives_inner, trivial_dropck_outlives, }; use crate::traits::ObligationCtxt; -use rustc_macros::{HashStable, TypeFoldable, TypeVisitable}; -use rustc_middle::traits::query::{DropckOutlivesResult, NoSolution}; -use rustc_middle::ty::{ParamEnvAnd, Ty, TyCtxt}; #[derive(Copy, Clone, Debug, HashStable, TypeFoldable, TypeVisitable)] pub struct DropckOutlives<'tcx> { diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/prove_predicate.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/prove_predicate.rs index 63289746f5e..294c6bfc124 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/prove_predicate.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/prove_predicate.rs @@ -1,11 +1,11 @@ -use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; -use crate::traits::ObligationCtxt; use rustc_infer::traits::Obligation; +pub use rustc_middle::traits::query::type_op::ProvePredicate; use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::ObligationCause; use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt}; -pub use rustc_middle::traits::query::type_op::ProvePredicate; +use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; +use crate::traits::ObligationCtxt; impl<'tcx> super::QueryTypeOp<'tcx> for ProvePredicate<'tcx> { type QueryResponse = (); diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/subtype.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/subtype.rs index ae11b0825bd..892c2a1f113 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/subtype.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/subtype.rs @@ -1,10 +1,10 @@ -use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; -use crate::traits::ObligationCtxt; +pub use rustc_middle::traits::query::type_op::Subtype; use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::ObligationCause; use rustc_middle::ty::{ParamEnvAnd, TyCtxt}; -pub use rustc_middle::traits::query::type_op::Subtype; +use crate::infer::canonical::{Canonical, CanonicalQueryResponse}; +use crate::traits::ObligationCtxt; impl<'tcx> super::QueryTypeOp<'tcx> for Subtype<'tcx> { type QueryResponse = (); diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 06b79ea63ca..9de62031311 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -12,20 +12,17 @@ use hir::def_id::DefId; use hir::LangItem; use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; use rustc_hir as hir; -use rustc_infer::traits::ObligationCause; -use rustc_infer::traits::{Obligation, PolyTraitObligation, SelectionError}; +use rustc_infer::traits::{Obligation, ObligationCause, PolyTraitObligation, SelectionError}; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::{self, ToPolyTraitRef, Ty, TypeVisitableExt}; use rustc_middle::{bug, span_bug}; +use super::SelectionCandidate::*; +use super::{BuiltinImplConditions, SelectionCandidateSet, SelectionContext, TraitObligationStack}; use crate::traits; use crate::traits::query::evaluate_obligation::InferCtxtExt; use crate::traits::util; -use super::BuiltinImplConditions; -use super::SelectionCandidate::*; -use super::{SelectionCandidateSet, SelectionContext, TraitObligationStack}; - impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { #[instrument(skip(self, stack), level = "debug")] pub(super) fn assemble_candidates<'o>( @@ -470,8 +467,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } candidates.vec.push(AsyncClosureCandidate); } - ty::FnDef(..) | ty::FnPtr(..) => { - candidates.vec.push(AsyncClosureCandidate); + // Provide an impl, but only for suitable `fn` pointers. + ty::FnPtr(sig) => { + if sig.is_fn_trait_compatible() { + candidates.vec.push(AsyncClosureCandidate); + } + } + // Provide an impl for suitable functions, rejecting `#[target_feature]` functions (RFC 2396). + ty::FnDef(def_id, _) => { + let tcx = self.tcx(); + if tcx.fn_sig(def_id).skip_binder().is_fn_trait_compatible() + && tcx.codegen_fn_attrs(def_id).target_features.is_empty() + { + candidates.vec.push(AsyncClosureCandidate); + } } _ => {} } diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 9508a3e8e15..ddd8b970cc8 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -7,11 +7,13 @@ //! [rustc dev guide]: //! https://rustc-dev-guide.rust-lang.org/traits/resolution.html#confirmation +use std::iter; +use std::ops::ControlFlow; + use rustc_ast::Mutability; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_hir::lang_items::LangItem; -use rustc_infer::infer::HigherRankedType; -use rustc_infer::infer::{DefineOpaqueTypes, InferOk}; +use rustc_infer::infer::{DefineOpaqueTypes, HigherRankedType, InferOk}; use rustc_infer::traits::ObligationCauseCode; use rustc_middle::traits::{BuiltinImplSource, SignatureMismatchData}; use rustc_middle::ty::{ @@ -21,6 +23,8 @@ use rustc_middle::ty::{ use rustc_middle::{bug, span_bug}; use rustc_span::def_id::DefId; +use super::SelectionCandidate::{self, *}; +use super::{BuiltinImplConditions, SelectionContext}; use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to}; use crate::traits::util::{self, closure_trait_ref_and_return_type}; use crate::traits::{ @@ -29,13 +33,6 @@ use crate::traits::{ SignatureMismatch, TraitNotObjectSafe, TraitObligation, Unimplemented, }; -use super::BuiltinImplConditions; -use super::SelectionCandidate::{self, *}; -use super::SelectionContext; - -use std::iter; -use std::ops::ControlFlow; - impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { #[instrument(level = "debug", skip(self))] pub(super) fn confirm_candidate( diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 699c05466bd..1b2767a6a62 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2,31 +2,12 @@ //! //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html#selection -use self::EvaluationResult::*; -use self::SelectionCandidate::*; - -use super::coherence::{self, Conflict}; -use super::const_evaluatable; -use super::project; -use super::project::ProjectionTermObligation; -use super::util; -use super::util::closure_trait_ref_and_return_type; -use super::wf; -use super::{ - ImplDerivedCause, Normalized, Obligation, ObligationCause, ObligationCauseCode, Overflow, - PolyTraitObligation, PredicateObligation, Selection, SelectionError, SelectionResult, - TraitQueryMode, -}; +use std::cell::{Cell, RefCell}; +use std::fmt::{self, Display}; +use std::ops::ControlFlow; +use std::{cmp, iter}; -use crate::error_reporting::InferCtxtErrorExt; -use crate::infer::{InferCtxt, InferCtxtExt, InferOk, TypeFreshener}; -use crate::solve::InferCtxtSelectExt as _; -use crate::traits::normalize::normalize_with_depth; -use crate::traits::normalize::normalize_with_depth_to; -use crate::traits::project::ProjectAndUnifyResult; -use crate::traits::project::ProjectionCacheKeyExt; -use crate::traits::ProjectionCacheKey; -use crate::traits::Unimplemented; +use hir::def::DefKind; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_errors::{Diag, EmissionGuarantee}; @@ -34,31 +15,39 @@ use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::LangItem; use rustc_infer::infer::relate::TypeRelation; -use rustc_infer::infer::BoundRegionConversionTime; -use rustc_infer::infer::BoundRegionConversionTime::HigherRankedType; +use rustc_infer::infer::BoundRegionConversionTime::{self, HigherRankedType}; use rustc_infer::infer::DefineOpaqueTypes; use rustc_infer::traits::TraitObligation; use rustc_middle::bug; -use rustc_middle::dep_graph::dep_kinds; -use rustc_middle::dep_graph::DepNodeIndex; +use rustc_middle::dep_graph::{dep_kinds, DepNodeIndex}; use rustc_middle::mir::interpret::ErrorHandled; +pub use rustc_middle::traits::select::*; use rustc_middle::ty::abstract_const::NotConstEvaluatable; use rustc_middle::ty::error::TypeErrorToStringExt; -use rustc_middle::ty::print::PrintTraitRefExt as _; -use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::{self, PolyProjectionPredicate, Upcast}; -use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable, TypeVisitableExt}; +use rustc_middle::ty::print::{with_no_trimmed_paths, PrintTraitRefExt as _}; +use rustc_middle::ty::{ + self, GenericArgsRef, PolyProjectionPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, + Upcast, +}; use rustc_span::symbol::sym; use rustc_span::Symbol; -use std::cell::{Cell, RefCell}; -use std::cmp; -use std::fmt::{self, Display}; -use std::iter; -use std::ops::ControlFlow; - -pub use rustc_middle::traits::select::*; -use rustc_middle::ty::print::with_no_trimmed_paths; +use self::EvaluationResult::*; +use self::SelectionCandidate::*; +use super::coherence::{self, Conflict}; +use super::project::ProjectionTermObligation; +use super::util::closure_trait_ref_and_return_type; +use super::{ + const_evaluatable, project, util, wf, ImplDerivedCause, Normalized, Obligation, + ObligationCause, ObligationCauseCode, Overflow, PolyTraitObligation, PredicateObligation, + Selection, SelectionError, SelectionResult, TraitQueryMode, +}; +use crate::error_reporting::InferCtxtErrorExt; +use crate::infer::{InferCtxt, InferCtxtExt, InferOk, TypeFreshener}; +use crate::solve::InferCtxtSelectExt as _; +use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to}; +use crate::traits::project::{ProjectAndUnifyResult, ProjectionCacheKeyExt}; +use crate::traits::{ProjectionCacheKey, Unimplemented}; mod _match; mod candidate_assembly; @@ -2810,6 +2799,26 @@ impl<'tcx> SelectionContext<'_, 'tcx> { }); } + // Register any outlives obligations from the trait here, cc #124336. + if matches!(tcx.def_kind(def_id), DefKind::Impl { of_trait: true }) { + for clause in tcx.impl_super_outlives(def_id).iter_instantiated(tcx, args) { + let clause = normalize_with_depth_to( + self, + param_env, + cause.clone(), + recursion_depth, + clause, + &mut obligations, + ); + obligations.push(Obligation { + cause: cause.clone(), + recursion_depth, + param_env, + predicate: clause.as_predicate(), + }); + } + } + obligations } } diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index 3c33d13567d..4c8c5a2eb17 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -10,28 +10,25 @@ //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html pub mod specialization_graph; +use rustc_data_structures::fx::FxIndexSet; +use rustc_errors::codes::*; +use rustc_errors::{Diag, EmissionGuarantee}; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_infer::infer::DefineOpaqueTypes; +use rustc_middle::bug; +use rustc_middle::query::LocalCrate; use rustc_middle::ty::print::PrintTraitRefExt as _; +use rustc_middle::ty::{self, GenericArgsRef, ImplSubject, Ty, TyCtxt, TypeVisitableExt}; +use rustc_session::lint::builtin::{COHERENCE_LEAK_CHECK, ORDER_DEPENDENT_TRAIT_OBJECTS}; +use rustc_span::{sym, ErrorGuaranteed, Span, DUMMY_SP}; use specialization_graph::GraphExt; +use super::{util, SelectionContext}; use crate::error_reporting::traits::to_pretty_impl_header; use crate::errors::NegativePositiveConflict; use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt}; use crate::traits::select::IntercrateAmbiguityCause; use crate::traits::{coherence, FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt}; -use rustc_data_structures::fx::FxIndexSet; -use rustc_errors::{codes::*, Diag, EmissionGuarantee}; -use rustc_hir::def_id::{DefId, LocalDefId}; -use rustc_middle::bug; -use rustc_middle::query::LocalCrate; -use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt, TypeVisitableExt}; -use rustc_session::lint::builtin::COHERENCE_LEAK_CHECK; -use rustc_session::lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS; -use rustc_span::{sym, ErrorGuaranteed, Span, DUMMY_SP}; - -use super::util; -use super::SelectionContext; /// Information pertinent to an overlapping impl error. #[derive(Debug)] diff --git a/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs b/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs index 90f2c7ad213..732f1b0a3d7 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs @@ -1,14 +1,13 @@ -use super::OverlapError; - -use crate::traits; use rustc_errors::ErrorGuaranteed; use rustc_hir::def_id::DefId; use rustc_macros::extension; use rustc_middle::bug; +pub use rustc_middle::traits::specialization_graph::*; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; -pub use rustc_middle::traits::specialization_graph::*; +use super::OverlapError; +use crate::traits; #[derive(Copy, Clone, Debug)] pub enum FutureCompatOverlapErrorKind { diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs index 951af4b0920..52f87699b16 100644 --- a/compiler/rustc_trait_selection/src/traits/util.rs +++ b/compiler/rustc_trait_selection/src/traits/util.rs @@ -1,19 +1,19 @@ use std::collections::BTreeMap; -use super::NormalizeExt; -use super::{ObligationCause, PredicateObligation, SelectionContext}; use rustc_data_structures::fx::FxIndexMap; use rustc_errors::Diag; use rustc_hir::def_id::DefId; use rustc_infer::infer::{InferCtxt, InferOk}; +pub use rustc_infer::traits::util::*; use rustc_middle::bug; -use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt, TypeVisitableExt, Upcast}; -use rustc_middle::ty::{TypeFoldable, TypeFolder, TypeSuperFoldable}; +use rustc_middle::ty::{ + self, GenericArgsRef, ImplSubject, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, + TypeVisitableExt, Upcast, +}; use rustc_span::Span; use smallvec::{smallvec, SmallVec}; -pub use rustc_infer::traits::util::*; +use super::{NormalizeExt, ObligationCause, PredicateObligation, SelectionContext}; /////////////////////////////////////////////////////////////////////////// // `TraitAliasExpander` iterator diff --git a/compiler/rustc_trait_selection/src/traits/vtable.rs b/compiler/rustc_trait_selection/src/traits/vtable.rs index 4645d828461..1729d8d307a 100644 --- a/compiler/rustc_trait_selection/src/traits/vtable.rs +++ b/compiler/rustc_trait_selection/src/traits/vtable.rs @@ -1,16 +1,18 @@ -use crate::errors::DumpVTableEntries; -use crate::traits::{impossible_predicates, is_vtable_safe_method}; +use std::fmt::Debug; +use std::ops::ControlFlow; + use rustc_hir::def_id::DefId; use rustc_infer::traits::util::PredicateSet; use rustc_middle::bug; use rustc_middle::query::Providers; -use rustc_middle::ty::{self, GenericParamDefKind, Ty, TyCtxt, Upcast, VtblEntry}; -use rustc_middle::ty::{GenericArgs, TypeVisitableExt}; +use rustc_middle::ty::{ + self, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt, Upcast, VtblEntry, +}; use rustc_span::{sym, Span, DUMMY_SP}; use smallvec::{smallvec, SmallVec}; -use std::fmt::Debug; -use std::ops::ControlFlow; +use crate::errors::DumpVTableEntries; +use crate::traits::{impossible_predicates, is_vtable_safe_method}; #[derive(Clone, Debug)] pub enum VtblSegment<'tcx> { diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index e77a05dd8e6..7e5fe7e3c94 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -1,17 +1,18 @@ -use crate::infer::InferCtxt; -use crate::traits; +use std::iter; + use rustc_hir as hir; use rustc_hir::lang_items::LangItem; use rustc_infer::traits::ObligationCauseCode; use rustc_middle::bug; use rustc_middle::ty::{ - self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, + self, GenericArg, GenericArgKind, GenericArgsRef, Ty, TyCtxt, TypeSuperVisitable, + TypeVisitable, TypeVisitableExt, TypeVisitor, }; -use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgsRef}; use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; use rustc_span::{Span, DUMMY_SP}; -use std::iter; +use crate::infer::InferCtxt; +use crate::traits; /// Returns the set of obligations needed to make `arg` well-formed. /// If `arg` contains unresolved inference variables, this may include /// further WF obligations. However, if `arg` IS an unresolved |
