Skip to content

Commit

Permalink
Nits and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 3, 2024
1 parent 1e72c7f commit a41c44f
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use rustc_middle::traits::query::NoSolution;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
use rustc_span::Span;
use rustc_trait_selection::solve::deeply_normalize;
use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput};
use rustc_trait_selection::traits::ScrubbedTraitError;
Expand Down Expand Up @@ -283,8 +282,9 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
) -> Ty<'tcx> {
let result = CustomTypeOp::new(
|ocx| {
deeply_normalize(
ocx.infcx.at(&ObligationCause::dummy_with_span(self.span), self.param_env),
ocx.deeply_normalize(
&ObligationCause::dummy_with_span(self.span),
self.param_env,
ty,
)
.map_err(|_: Vec<ScrubbedTraitError<'tcx>>| NoSolution)
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::intravisit;
use rustc_hir::{GenericParamKind, ImplItemKind};
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::{util, FulfillmentErrorLike};
use rustc_infer::traits::util;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::fold::BottomUpFolder;
use rustc_middle::ty::util::ExplicitSelf;
Expand Down Expand Up @@ -764,10 +764,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
Ok(&*tcx.arena.alloc(remapped_types))
}

struct ImplTraitInTraitCollector<'a, 'tcx, E>
where
E: FulfillmentErrorLike<'tcx>,
{
struct ImplTraitInTraitCollector<'a, 'tcx, E> {
ocx: &'a ObligationCtxt<'a, 'tcx, E>,
types: FxIndexMap<DefId, (Ty<'tcx>, ty::GenericArgsRef<'tcx>)>,
span: Span,
Expand All @@ -777,7 +774,7 @@ where

impl<'a, 'tcx, E> ImplTraitInTraitCollector<'a, 'tcx, E>
where
E: FulfillmentErrorLike<'tcx>,
E: 'tcx,
{
fn new(
ocx: &'a ObligationCtxt<'a, 'tcx, E>,
Expand All @@ -791,7 +788,7 @@ where

impl<'tcx, E> TypeFolder<TyCtxt<'tcx>> for ImplTraitInTraitCollector<'_, 'tcx, E>
where
E: FulfillmentErrorLike<'tcx>,
E: 'tcx,
{
fn interner(&self) -> TyCtxt<'tcx> {
self.ocx.infcx.tcx
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ use rustc_middle::ty::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
use rustc_middle::{bug, span_bug};
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_trait_selection::traits::{
self, IsFirstInputType, ScrubbedTraitError, UncoveredTyParams,
};
use rustc_trait_selection::traits::{self, IsFirstInputType, UncoveredTyParams};
use rustc_trait_selection::traits::{OrphanCheckErr, OrphanCheckMode};
use rustc_trait_selection::traits::{StructurallyNormalizeExt, TraitEngineExt};

#[instrument(level = "debug", skip(tcx))]
pub(crate) fn orphan_check_impl(
Expand Down Expand Up @@ -319,13 +316,12 @@ fn orphan_check<'tcx>(
}

let ty = if infcx.next_trait_solver() {
let mut fulfill_cx =
<dyn traits::TraitEngine<'tcx, ScrubbedTraitError<'tcx>>>::new(&infcx);
infcx
.at(&cause, ty::ParamEnv::empty())
.structurally_normalize(ty, &mut *fulfill_cx)
.map(|ty| infcx.resolve_vars_if_possible(ty))
.unwrap_or(ty)
ocx.structurally_normalize(
&cause,
ty::ParamEnv::empty(),
infcx.resolve_vars_if_possible(ty),
)
.unwrap_or(ty)
} else {
ty
};
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::infer::canonical::{
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
use crate::infer::{DefineOpaqueTypes, InferCtxt, InferOk, InferResult};
use crate::traits::query::NoSolution;
use crate::traits::{FulfillmentErrorLike, TraitEngine};
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
use crate::traits::{ScrubbedTraitError, TraitEngine};
use rustc_data_structures::captures::Captures;
use rustc_index::Idx;
use rustc_index::IndexVec;
Expand Down Expand Up @@ -50,11 +50,11 @@ impl<'tcx> InferCtxt<'tcx> {
/// - Finally, if any of the obligations result in a hard error,
/// then `Err(NoSolution)` is returned.
#[instrument(skip(self, inference_vars, answer, fulfill_cx), level = "trace")]
pub fn make_canonicalized_query_response<T, E: FulfillmentErrorLike<'tcx>>(
pub fn make_canonicalized_query_response<T>(
&self,
inference_vars: CanonicalVarValues<'tcx>,
answer: T,
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>,
) -> Result<CanonicalQueryResponse<'tcx, T>, NoSolution>
where
T: Debug + TypeFoldable<TyCtxt<'tcx>>,
Expand Down Expand Up @@ -97,11 +97,11 @@ impl<'tcx> InferCtxt<'tcx> {
/// Helper for `make_canonicalized_query_response` that does
/// everything up until the final canonicalization.
#[instrument(skip(self, fulfill_cx), level = "debug")]
fn make_query_response<T, E: FulfillmentErrorLike<'tcx>>(
fn make_query_response<T>(
&self,
inference_vars: CanonicalVarValues<'tcx>,
answer: T,
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>,
) -> Result<QueryResponse<'tcx, T>, NoSolution>
where
T: Debug + TypeFoldable<TyCtxt<'tcx>>,
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ pub use SubregionOrigin::*;
pub use ValuePairs::*;

use crate::traits::{
self, FulfillmentErrorLike, ObligationCause, ObligationInspector, PredicateObligations,
TraitEngine,
self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine,
};
use error_reporting::TypeErrCtxt;
use free_regions::RegionRelations;
Expand Down Expand Up @@ -738,7 +737,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {

impl<'tcx, T> InferOk<'tcx, T> {
/// Extracts `value`, registering any obligations into `fulfill_cx`.
pub fn into_value_registering_obligations<E: FulfillmentErrorLike<'tcx>>(
pub fn into_value_registering_obligations<E: 'tcx>(
self,
infcx: &InferCtxt<'tcx>,
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
Expand Down
33 changes: 27 additions & 6 deletions compiler/rustc_infer/src/traits/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,32 @@ use rustc_middle::ty::{self, Ty, Upcast};

use super::{ObligationCause, PredicateObligation};

pub trait TraitEngine<'tcx, E: FulfillmentErrorLike<'tcx>>: 'tcx {
/// A trait error with most of its information removed. This is the error
/// returned by an `ObligationCtxt` by default, and suitable if you just
/// want to see if a predicate holds, and don't particularly care about the
/// error itself (except for if it's an ambiguity or true error).
///
/// use `ObligationCtxt::new_with_diagnostics` to get a `FulfillmentError`.
#[derive(Clone, Debug)]
pub enum ScrubbedTraitError<'tcx> {
/// A real error. This goal definitely does not hold.
TrueError,
/// An ambiguity. This goal may hold if further inference is done.
Ambiguity,
/// An old-solver-style cycle error, which will fatal.
Cycle(Vec<PredicateObligation<'tcx>>),
}

impl<'tcx> ScrubbedTraitError<'tcx> {
pub fn is_true_error(&self) -> bool {
match self {
ScrubbedTraitError::TrueError => true,
ScrubbedTraitError::Ambiguity | ScrubbedTraitError::Cycle(_) => false,
}
}
}

pub trait TraitEngine<'tcx, E: 'tcx>: 'tcx {
/// Requires that `ty` must implement the trait with `def_id` in
/// the given environment. This trait must not have any type
/// parameters (except for `Self`).
Expand Down Expand Up @@ -73,10 +98,6 @@ pub trait TraitEngine<'tcx, E: FulfillmentErrorLike<'tcx>>: 'tcx {
) -> Vec<PredicateObligation<'tcx>>;
}

pub trait FulfillmentErrorLike<'tcx>: Debug + 'tcx {
fn is_true_error(&self) -> bool;
}

pub trait FromSolverError<'tcx, E>: FulfillmentErrorLike<'tcx> {
pub trait FromSolverError<'tcx, E>: Debug + 'tcx {
fn from_solver_error(infcx: &InferCtxt<'tcx>, error: E) -> Self;
}
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use self::ImplSource::*;
pub use self::SelectionError::*;
use crate::infer::InferCtxt;

pub use self::engine::{FromSolverError, FulfillmentErrorLike, TraitEngine};
pub use self::engine::{FromSolverError, ScrubbedTraitError, TraitEngine};
pub use self::project::MismatchedProjectionTypes;
pub(crate) use self::project::UndoLog;
pub use self::project::{
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_macros/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use syn::spanned::Spanned;
use syn::{
braced, parse_macro_input, Attribute, Generics, ImplItem, Pat, PatIdent, Path, Signature,
Token, TraitItem, TraitItemConst, TraitItemFn, TraitItemMacro, TraitItemType, Type, Visibility,
WhereClause,
};

pub(crate) fn extension(
attr: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let ExtensionAttr { vis, trait_ } = parse_macro_input!(attr as ExtensionAttr);
let Impl { attrs, generics, self_ty, items } = parse_macro_input!(input as Impl);
let Impl { attrs, generics, self_ty, items, wc } = parse_macro_input!(input as Impl);
let headers: Vec<_> = items
.iter()
.map(|item| match item {
Expand Down Expand Up @@ -59,7 +60,7 @@ pub(crate) fn extension(
#(#headers)*
}

impl #generics #trait_ for #self_ty {
impl #generics #trait_ for #self_ty #wc {
#(#items)*
}
}
Expand Down Expand Up @@ -133,6 +134,7 @@ struct Impl {
generics: Generics,
self_ty: Type,
items: Vec<ImplItem>,
wc: Option<WhereClause>,
}

impl Parse for Impl {
Expand All @@ -141,6 +143,7 @@ impl Parse for Impl {
let _: Token![impl] = input.parse()?;
let generics = input.parse()?;
let self_ty = input.parse()?;
let wc = input.parse()?;

let content;
let _brace_token = braced!(content in input);
Expand All @@ -149,6 +152,6 @@ impl Parse for Impl {
items.push(content.parse()?);
}

Ok(Impl { attrs, generics, self_ty, items })
Ok(Impl { attrs, generics, self_ty, items, wc })
}
}
13 changes: 7 additions & 6 deletions compiler/rustc_trait_selection/src/solve/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rustc_infer::infer::InferCtxt;
use rustc_infer::traits::query::NoSolution;
use rustc_infer::traits::solve::{CandidateSource, GoalSource, MaybeCause};
use rustc_infer::traits::{
self, FromSolverError, FulfillmentErrorLike, MismatchedProjectionTypes, Obligation,
ObligationCause, ObligationCauseCode, PredicateObligation, SelectionError, TraitEngine,
self, FromSolverError, MismatchedProjectionTypes, Obligation, ObligationCause,
ObligationCauseCode, PredicateObligation, SelectionError, TraitEngine,
};
use rustc_middle::bug;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
Expand All @@ -31,7 +31,7 @@ use super::{Certainty, InferCtxtEvalExt};
///
/// It is also likely that we want to use slightly different datastructures
/// here as this will have to deal with far more root goals than `evaluate_all`.
pub struct FulfillmentCtxt<'tcx, E: FulfillmentErrorLike<'tcx>> {
pub struct FulfillmentCtxt<'tcx, E: 'tcx> {
obligations: ObligationStorage<'tcx>,

/// The snapshot in which this context was created. Using the context
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<'tcx> ObligationStorage<'tcx> {
}
}

impl<'tcx, E: FulfillmentErrorLike<'tcx>> FulfillmentCtxt<'tcx, E> {
impl<'tcx, E: 'tcx> FulfillmentCtxt<'tcx, E> {
pub fn new(infcx: &InferCtxt<'tcx>) -> FulfillmentCtxt<'tcx, E> {
assert!(
infcx.next_trait_solver(),
Expand Down Expand Up @@ -123,8 +123,9 @@ impl<'tcx, E: FulfillmentErrorLike<'tcx>> FulfillmentCtxt<'tcx, E> {
}
}

impl<'tcx, E: FromSolverError<'tcx, NextSolverError<'tcx>>> TraitEngine<'tcx, E>
for FulfillmentCtxt<'tcx, E>
impl<'tcx, E> TraitEngine<'tcx, E> for FulfillmentCtxt<'tcx, E>
where
E: FromSolverError<'tcx, NextSolverError<'tcx>>,
{
#[instrument(level = "trace", skip(self, infcx))]
fn register_predicate_obligation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use rustc_ast_ir::try_visit;
use rustc_ast_ir::visit::VisitorResult;
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk};
use rustc_infer::traits::FulfillmentErrorLike as _;
use rustc_macros::extension;
use rustc_middle::traits::query::NoSolution;
use rustc_middle::traits::solve::{inspect, QueryResult};
Expand Down
36 changes: 19 additions & 17 deletions compiler/rustc_trait_selection/src/solve/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::marker::PhantomData;

use crate::traits::error_reporting::{OverflowCause, TypeErrCtxtExt};
Expand All @@ -6,7 +7,7 @@ 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, FulfillmentErrorLike, Obligation, TraitEngine};
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};
Expand All @@ -16,14 +17,11 @@ use super::{FulfillmentCtxt, NextSolverError};

/// Deeply normalize all aliases in `value`. This does not handle inference and expects
/// its input to be already fully resolved.
pub fn deeply_normalize<
'tcx,
pub fn deeply_normalize<'tcx, T, E>(at: At<'_, 'tcx>, value: T) -> Result<T, Vec<E>>
where
T: TypeFoldable<TyCtxt<'tcx>>,
E: FromSolverError<'tcx, NextSolverError<'tcx>>,
>(
at: At<'_, 'tcx>,
value: T,
) -> Result<T, Vec<E>> {
{
assert!(!value.has_escaping_bound_vars());
deeply_normalize_with_skipped_universes(at, value, vec![])
}
Expand All @@ -34,31 +32,34 @@ pub fn deeply_normalize<
/// Additionally takes a list of universes which represents the binders which have been
/// entered before passing `value` to the function. This is currently needed for
/// `normalize_erasing_regions`, which skips binders as it walks through a type.
pub fn deeply_normalize_with_skipped_universes<
'tcx,
T: TypeFoldable<TyCtxt<'tcx>>,
E: FromSolverError<'tcx, NextSolverError<'tcx>>,
>(
pub fn deeply_normalize_with_skipped_universes<'tcx, T, E>(
at: At<'_, 'tcx>,
value: T,
universes: Vec<Option<UniverseIndex>>,
) -> Result<T, Vec<E>> {
) -> Result<T, Vec<E>>
where
T: TypeFoldable<TyCtxt<'tcx>>,
E: FromSolverError<'tcx, NextSolverError<'tcx>>,
{
let fulfill_cx = FulfillmentCtxt::new(at.infcx);
let mut folder =
NormalizationFolder { at, fulfill_cx, depth: 0, universes, _errors: PhantomData };

value.try_fold_with(&mut folder)
}

struct NormalizationFolder<'me, 'tcx, E: FulfillmentErrorLike<'tcx>> {
struct NormalizationFolder<'me, 'tcx, E> {
at: At<'me, 'tcx>,
fulfill_cx: FulfillmentCtxt<'tcx, E>,
depth: usize,
universes: Vec<Option<UniverseIndex>>,
_errors: PhantomData<E>,
}

impl<'tcx, E: FromSolverError<'tcx, NextSolverError<'tcx>>> NormalizationFolder<'_, 'tcx, E> {
impl<'tcx, E> NormalizationFolder<'_, 'tcx, E>
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(..)));

Expand Down Expand Up @@ -150,8 +151,9 @@ impl<'tcx, E: FromSolverError<'tcx, NextSolverError<'tcx>>> NormalizationFolder<
}
}

impl<'tcx, E: FromSolverError<'tcx, NextSolverError<'tcx>>> FallibleTypeFolder<TyCtxt<'tcx>>
for NormalizationFolder<'_, 'tcx, E>
impl<'tcx, E> FallibleTypeFolder<TyCtxt<'tcx>> for NormalizationFolder<'_, 'tcx, E>
where
E: FromSolverError<'tcx, NextSolverError<'tcx>> + Debug,
{
type Error = Vec<E>;

Expand Down
Loading

0 comments on commit a41c44f

Please sign in to comment.