Skip to content

Commit 2b730a3

Browse files
committed
remove impl_implied_bounds from FnCtxt
1 parent 7808f69 commit 2b730a3

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

compiler/rustc_typeck/src/check/compare_method.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use super::{potentially_plural_count, Inherited};
12
use crate::check::regionck::OutlivesEnvironmentExt;
3+
use crate::check::wfcheck;
24
use crate::errors::LifetimesOrBoundsMismatchOnTrait;
35
use rustc_data_structures::stable_set::FxHashSet;
46
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorGuaranteed};
@@ -19,8 +21,6 @@ use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
1921
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, Reveal};
2022
use std::iter;
2123

22-
use super::{potentially_plural_count, FnCtxt, Inherited};
23-
2424
/// Checks that a method from an impl conforms to the signature of
2525
/// the same method as declared in the trait.
2626
///
@@ -1491,12 +1491,11 @@ pub fn check_type_bounds<'tcx>(
14911491

14921492
// Finally, resolve all regions. This catches wily misuses of
14931493
// lifetime parameters.
1494-
//
1495-
// FIXME: Remove that `FnCtxt`.
1496-
let fcx = FnCtxt::new(&inh, param_env, impl_ty_hir_id);
14971494
let implied_bounds = match impl_ty.container {
14981495
ty::TraitContainer(_) => FxHashSet::default(),
1499-
ty::ImplContainer(def_id) => fcx.impl_implied_bounds(def_id, impl_ty_span),
1496+
ty::ImplContainer(def_id) => {
1497+
wfcheck::impl_implied_bounds(tcx, param_env, def_id.expect_local(), impl_ty_span)
1498+
}
15001499
};
15011500
let mut outlives_environment = OutlivesEnvironment::new(param_env);
15021501
outlives_environment.add_implied_bounds(infcx, implied_bounds, impl_ty_hir_id);

compiler/rustc_typeck/src/check/wfcheck.rs

+32-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::check::regionck::OutlivesEnvironmentExt;
22
use crate::check::{FnCtxt, Inherited};
33
use crate::constrained_generic_params::{identify_constrained_generic_params, Parameter};
4-
54
use rustc_ast as ast;
65
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
76
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
@@ -13,6 +12,7 @@ use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1312
use rustc_infer::infer::outlives::obligations::TypeOutlives;
1413
use rustc_infer::infer::region_constraints::GenericKind;
1514
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
15+
use rustc_infer::traits::Normalized;
1616
use rustc_middle::ty::query::Providers;
1717
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts, Subst};
1818
use rustc_middle::ty::trait_def::TraitSpecializationKind;
@@ -24,7 +24,9 @@ use rustc_session::parse::feature_err;
2424
use rustc_span::symbol::{sym, Ident, Symbol};
2525
use rustc_span::{Span, DUMMY_SP};
2626
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
27+
use rustc_trait_selection::traits::query::normalize::AtExt;
2728
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, WellFormedLoc};
29+
use rustc_trait_selection::traits::query::NoSolution;
2830

2931
use std::cell::LazyCell;
3032
use std::convert::TryInto;
@@ -939,9 +941,10 @@ fn check_associated_item(
939941

940942
let (mut implied_bounds, self_ty) = match item.container {
941943
ty::TraitContainer(_) => (FxHashSet::default(), fcx.tcx.types.self_param),
942-
ty::ImplContainer(def_id) => {
943-
(fcx.impl_implied_bounds(def_id, span), fcx.tcx.type_of(def_id))
944-
}
944+
ty::ImplContainer(def_id) => (
945+
impl_implied_bounds(tcx, fcx.param_env, def_id.expect_local(), span),
946+
fcx.tcx.type_of(def_id),
947+
),
945948
};
946949

947950
match item.kind {
@@ -1259,7 +1262,7 @@ fn check_impl<'tcx>(
12591262

12601263
check_where_clauses(fcx, item.span, item.def_id, None);
12611264

1262-
fcx.impl_implied_bounds(item.def_id.to_def_id(), item.span)
1265+
impl_implied_bounds(tcx, fcx.param_env, item.def_id, item.span)
12631266
});
12641267
}
12651268

@@ -1917,28 +1920,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19171920
})
19181921
.collect()
19191922
}
1923+
}
19201924

1921-
pub(super) fn impl_implied_bounds(
1922-
&self,
1923-
impl_def_id: DefId,
1924-
span: Span,
1925-
) -> FxHashSet<Ty<'tcx>> {
1926-
match self.tcx.impl_trait_ref(impl_def_id) {
1925+
pub(super) fn impl_implied_bounds<'tcx>(
1926+
tcx: TyCtxt<'tcx>,
1927+
param_env: ty::ParamEnv<'tcx>,
1928+
impl_def_id: LocalDefId,
1929+
span: Span,
1930+
) -> FxHashSet<Ty<'tcx>> {
1931+
// We completely ignore any obligations caused by normalizing the types
1932+
// we assume to be well formed. Considering that the user of the implied
1933+
// bounds will also normalize them, we leave it to them to emit errors
1934+
// which should result in better causes and spans.
1935+
tcx.infer_ctxt().enter(|infcx| {
1936+
let cause = ObligationCause::misc(span, tcx.hir().local_def_id_to_hir_id(impl_def_id));
1937+
match tcx.impl_trait_ref(impl_def_id) {
19271938
Some(trait_ref) => {
19281939
// Trait impl: take implied bounds from all types that
19291940
// appear in the trait reference.
1930-
let trait_ref = self.normalize_associated_types_in(span, trait_ref);
1931-
trait_ref.substs.types().collect()
1941+
match infcx.at(&cause, param_env).normalize(trait_ref) {
1942+
Ok(Normalized { value, obligations: _ }) => value.substs.types().collect(),
1943+
Err(NoSolution) => FxHashSet::default(),
1944+
}
19321945
}
19331946

19341947
None => {
19351948
// Inherent impl: take implied bounds from the `self` type.
1936-
let self_ty = self.tcx.type_of(impl_def_id);
1937-
let self_ty = self.normalize_associated_types_in(span, self_ty);
1938-
FxHashSet::from_iter([self_ty])
1949+
let self_ty = tcx.type_of(impl_def_id);
1950+
match infcx.at(&cause, param_env).normalize(self_ty) {
1951+
Ok(Normalized { value, obligations: _ }) => FxHashSet::from_iter([value]),
1952+
Err(NoSolution) => FxHashSet::default(),
1953+
}
19391954
}
19401955
}
1941-
}
1956+
})
19421957
}
19431958

19441959
fn error_392(

src/test/ui/specialization/min_specialization/issue-79224.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ LL | impl<B: ?Sized + std::clone::Clone> Display for Cow<'_, B> {
1111
| +++++++++++++++++++
1212

1313
error[E0277]: the trait bound `B: Clone` is not satisfied
14-
--> $DIR/issue-79224.rs:19:5
14+
--> $DIR/issue-79224.rs:19:12
1515
|
16-
LL | / fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17-
LL | | write!(f, "foo")
18-
LL | | }
19-
| |_____^ the trait `Clone` is not implemented for `B`
16+
LL | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17+
| ^^^^^ the trait `Clone` is not implemented for `B`
2018
|
2119
= note: required because of the requirements on the impl of `ToOwned` for `B`
2220
help: consider further restricting this bound

0 commit comments

Comments
 (0)