Skip to content

Commit 475c71d

Browse files
committed
Auto merge of rust-lang#116640 - matthiaskrgr:rollup-xt9r5ir, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#116219 (Relate alias ty with variance) - rust-lang#116315 (Do not check for impossible predicates in const-prop lint.) - rust-lang#116436 (Structurally normalize for closure) - rust-lang#116597 (Prevent showing methods from blanket impls of not available foreign traits to show up in the search results) - rust-lang#116627 (small cleanup) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c1691db + 8374805 commit 475c71d

File tree

14 files changed

+132
-108
lines changed

14 files changed

+132
-108
lines changed

compiler/rustc_ast_lowering/src/item.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1623,12 +1623,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
16231623
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
16241624
bounded_ty: self
16251625
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1626-
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
1627-
self.lower_param_bound(
1628-
bound,
1629-
&ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1630-
)
1631-
})),
1626+
bounds: self.lower_param_bounds(
1627+
bounds,
1628+
&ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1629+
),
16321630
span: self.lower_span(*span),
16331631
origin: PredicateOrigin::WhereClause,
16341632
}),

compiler/rustc_hir_typeck/src/closure.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5656
// closure sooner rather than later, so first examine the expected
5757
// type, and see if can glean a closure kind from there.
5858
let (expected_sig, expected_kind) = match expected.to_option(self) {
59-
Some(ty) => self.deduce_closure_signature(ty),
59+
Some(ty) => {
60+
self.deduce_closure_signature(self.try_structurally_resolve_type(expr_span, ty))
61+
}
6062
None => (None, None),
6163
};
6264
let body = self.tcx.hir().body(closure.body);
@@ -688,8 +690,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
688690
span_bug!(self.tcx.def_span(expr_def_id), "async fn generator outside of a fn")
689691
});
690692

693+
let closure_span = self.tcx.def_span(expr_def_id);
691694
let ret_ty = ret_coercion.borrow().expected_ty();
692-
let ret_ty = self.inh.infcx.shallow_resolve(ret_ty);
695+
let ret_ty = self.try_structurally_resolve_type(closure_span, ret_ty);
693696

694697
let get_future_output = |predicate: ty::Predicate<'tcx>, span| {
695698
// Search for a pending obligation like
@@ -711,8 +714,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
711714
}
712715
};
713716

714-
let span = self.tcx.def_span(expr_def_id);
715-
716717
let output_ty = match *ret_ty.kind() {
717718
ty::Infer(ty::TyVar(ret_vid)) => {
718719
self.obligations_for_self_ty(ret_vid).find_map(|obligation| {
@@ -726,17 +727,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
726727
.find_map(|(p, s)| get_future_output(p.as_predicate(), s))?,
727728
ty::Error(_) => return None,
728729
_ => span_bug!(
729-
span,
730+
closure_span,
730731
"async fn generator return type not an inference variable: {ret_ty}"
731732
),
732733
};
733734

734-
let output_ty = self.normalize(span, output_ty);
735+
let output_ty = self.normalize(closure_span, output_ty);
735736

736737
// async fn that have opaque types in their return type need to redo the conversion to inference variables
737738
// as they fetch the still opaque version from the signature.
738739
let InferOk { value: output_ty, obligations } = self
739-
.replace_opaque_types_with_inference_vars(output_ty, body_def_id, span, self.param_env);
740+
.replace_opaque_types_with_inference_vars(
741+
output_ty,
742+
body_def_id,
743+
closure_span,
744+
self.param_env,
745+
);
740746
self.register_predicates(obligations);
741747

742748
Some(output_ty)

compiler/rustc_infer/src/infer/equate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
5656
// performing trait matching (which then performs equality
5757
// unification).
5858

59-
relate::relate_args(self, a_arg, b_arg)
59+
relate::relate_args_invariantly(self, a_arg, b_arg)
6060
}
6161

6262
fn relate_with_variance<T: Relate<'tcx>>(

compiler/rustc_infer/src/infer/generalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ where
183183
// Avoid fetching the variance if we are in an invariant
184184
// context; no need, and it can induce dependency cycles
185185
// (e.g., #41849).
186-
relate::relate_args(self, a_subst, b_subst)
186+
relate::relate_args_invariantly(self, a_subst, b_subst)
187187
} else {
188188
let tcx = self.tcx();
189189
let opt_variances = tcx.variances_of(item_def_id);

compiler/rustc_middle/src/ty/relate.rs

+24-41
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::ty::error::{ExpectedFound, TypeError};
88
use crate::ty::{self, Expr, ImplSubject, Term, TermKind, Ty, TyCtxt, TypeFoldable};
99
use crate::ty::{GenericArg, GenericArgKind, GenericArgsRef};
1010
use rustc_hir as hir;
11+
use rustc_hir::def::DefKind;
1112
use rustc_hir::def_id::DefId;
1213
use rustc_target::spec::abi;
1314
use std::iter;
@@ -134,7 +135,7 @@ pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>(
134135
}
135136

136137
#[inline]
137-
pub fn relate_args<'tcx, R: TypeRelation<'tcx>>(
138+
pub fn relate_args_invariantly<'tcx, R: TypeRelation<'tcx>>(
138139
relation: &mut R,
139140
a_arg: GenericArgsRef<'tcx>,
140141
b_arg: GenericArgsRef<'tcx>,
@@ -273,7 +274,20 @@ impl<'tcx> Relate<'tcx> for ty::AliasTy<'tcx> {
273274
if a.def_id != b.def_id {
274275
Err(TypeError::ProjectionMismatched(expected_found(relation, a.def_id, b.def_id)))
275276
} else {
276-
let args = relation.relate(a.args, b.args)?;
277+
let args = match relation.tcx().def_kind(a.def_id) {
278+
DefKind::OpaqueTy => relate_args_with_variances(
279+
relation,
280+
a.def_id,
281+
relation.tcx().variances_of(a.def_id),
282+
a.args,
283+
b.args,
284+
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
285+
)?,
286+
DefKind::AssocTy | DefKind::AssocConst | DefKind::TyAlias => {
287+
relate_args_invariantly(relation, a.args, b.args)?
288+
}
289+
def => bug!("unknown alias DefKind: {def:?}"),
290+
};
277291
Ok(relation.tcx().mk_alias_ty(a.def_id, args))
278292
}
279293
}
@@ -315,7 +329,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
315329
if a.def_id != b.def_id {
316330
Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
317331
} else {
318-
let args = relate_args(relation, a.args, b.args)?;
332+
let args = relate_args_invariantly(relation, a.args, b.args)?;
319333
Ok(ty::TraitRef::new(relation.tcx(), a.def_id, args))
320334
}
321335
}
@@ -331,7 +345,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
331345
if a.def_id != b.def_id {
332346
Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
333347
} else {
334-
let args = relate_args(relation, a.args, b.args)?;
348+
let args = relate_args_invariantly(relation, a.args, b.args)?;
335349
Ok(ty::ExistentialTraitRef { def_id: a.def_id, args })
336350
}
337351
}
@@ -449,7 +463,7 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>(
449463
// All Generator types with the same id represent
450464
// the (anonymous) type of the same generator expression. So
451465
// all of their regions should be equated.
452-
let args = relation.relate(a_args, b_args)?;
466+
let args = relate_args_invariantly(relation, a_args, b_args)?;
453467
Ok(Ty::new_generator(tcx, a_id, args, movability))
454468
}
455469

@@ -459,15 +473,15 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>(
459473
// All GeneratorWitness types with the same id represent
460474
// the (anonymous) type of the same generator expression. So
461475
// all of their regions should be equated.
462-
let args = relation.relate(a_args, b_args)?;
476+
let args = relate_args_invariantly(relation, a_args, b_args)?;
463477
Ok(Ty::new_generator_witness(tcx, a_id, args))
464478
}
465479

466480
(&ty::Closure(a_id, a_args), &ty::Closure(b_id, b_args)) if a_id == b_id => {
467481
// All Closure types with the same id represent
468482
// the (anonymous) type of the same closure expression. So
469483
// all of their regions should be equated.
470-
let args = relation.relate(a_args, b_args)?;
484+
let args = relate_args_invariantly(relation, a_args, b_args)?;
471485
Ok(Ty::new_closure(tcx, a_id, &args))
472486
}
473487

@@ -536,24 +550,6 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>(
536550
Ok(Ty::new_fn_ptr(tcx, fty))
537551
}
538552

539-
// The args of opaque types may not all be invariant, so we have
540-
// to treat them separately from other aliases.
541-
(
542-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, args: a_args, .. }),
543-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, args: b_args, .. }),
544-
) if a_def_id == b_def_id => {
545-
let opt_variances = tcx.variances_of(a_def_id);
546-
let args = relate_args_with_variances(
547-
relation,
548-
a_def_id,
549-
opt_variances,
550-
a_args,
551-
b_args,
552-
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
553-
)?;
554-
Ok(Ty::new_opaque(tcx, a_def_id, args))
555-
}
556-
557553
// Alias tend to mostly already be handled downstream due to normalization.
558554
(&ty::Alias(a_kind, a_data), &ty::Alias(b_kind, b_data)) => {
559555
let alias_ty = relation.relate(a_data, b_data)?;
@@ -709,7 +705,7 @@ impl<'tcx> Relate<'tcx> for ty::ClosureArgs<'tcx> {
709705
a: ty::ClosureArgs<'tcx>,
710706
b: ty::ClosureArgs<'tcx>,
711707
) -> RelateResult<'tcx, ty::ClosureArgs<'tcx>> {
712-
let args = relate_args(relation, a.args, b.args)?;
708+
let args = relate_args_invariantly(relation, a.args, b.args)?;
713709
Ok(ty::ClosureArgs { args })
714710
}
715711
}
@@ -720,7 +716,7 @@ impl<'tcx> Relate<'tcx> for ty::GeneratorArgs<'tcx> {
720716
a: ty::GeneratorArgs<'tcx>,
721717
b: ty::GeneratorArgs<'tcx>,
722718
) -> RelateResult<'tcx, ty::GeneratorArgs<'tcx>> {
723-
let args = relate_args(relation, a.args, b.args)?;
719+
let args = relate_args_invariantly(relation, a.args, b.args)?;
724720
Ok(ty::GeneratorArgs { args })
725721
}
726722
}
@@ -731,7 +727,7 @@ impl<'tcx> Relate<'tcx> for GenericArgsRef<'tcx> {
731727
a: GenericArgsRef<'tcx>,
732728
b: GenericArgsRef<'tcx>,
733729
) -> RelateResult<'tcx, GenericArgsRef<'tcx>> {
734-
relate_args(relation, a, b)
730+
relate_args_invariantly(relation, a, b)
735731
}
736732
}
737733

@@ -835,19 +831,6 @@ impl<'tcx> Relate<'tcx> for Term<'tcx> {
835831
}
836832
}
837833

838-
impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
839-
fn relate<R: TypeRelation<'tcx>>(
840-
relation: &mut R,
841-
a: ty::ProjectionPredicate<'tcx>,
842-
b: ty::ProjectionPredicate<'tcx>,
843-
) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>> {
844-
Ok(ty::ProjectionPredicate {
845-
projection_ty: relation.relate(a.projection_ty, b.projection_ty)?,
846-
term: relation.relate(a.term, b.term)?,
847-
})
848-
}
849-
}
850-
851834
///////////////////////////////////////////////////////////////////////////
852835
// Error handling
853836

compiler/rustc_mir_transform/src/const_prop_lint.rs

+10-47
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc_middle::ty::{
2222
};
2323
use rustc_span::Span;
2424
use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
25-
use rustc_trait_selection::traits;
2625

2726
use crate::const_prop::CanConstProp;
2827
use crate::const_prop::ConstPropMachine;
@@ -35,9 +34,9 @@ use crate::MirLint;
3534
/// Severely regress performance.
3635
const MAX_ALLOC_LIMIT: u64 = 1024;
3736

38-
pub struct ConstProp;
37+
pub struct ConstPropLint;
3938

40-
impl<'tcx> MirLint<'tcx> for ConstProp {
39+
impl<'tcx> MirLint<'tcx> for ConstPropLint {
4140
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
4241
if body.tainted_by_errors.is_some() {
4342
return;
@@ -49,61 +48,25 @@ impl<'tcx> MirLint<'tcx> for ConstProp {
4948
}
5049

5150
let def_id = body.source.def_id().expect_local();
52-
let is_fn_like = tcx.def_kind(def_id).is_fn_like();
53-
let is_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst;
51+
let def_kind = tcx.def_kind(def_id);
52+
let is_fn_like = def_kind.is_fn_like();
53+
let is_assoc_const = def_kind == DefKind::AssocConst;
5454

5555
// Only run const prop on functions, methods, closures and associated constants
5656
if !is_fn_like && !is_assoc_const {
5757
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
58-
trace!("ConstProp skipped for {:?}", def_id);
58+
trace!("ConstPropLint skipped for {:?}", def_id);
5959
return;
6060
}
6161

62-
let is_generator = tcx.type_of(def_id.to_def_id()).instantiate_identity().is_generator();
6362
// FIXME(welseywiser) const prop doesn't work on generators because of query cycles
6463
// computing their layout.
65-
if is_generator {
66-
trace!("ConstProp skipped for generator {:?}", def_id);
64+
if let DefKind::Generator = def_kind {
65+
trace!("ConstPropLint skipped for generator {:?}", def_id);
6766
return;
6867
}
6968

70-
// Check if it's even possible to satisfy the 'where' clauses
71-
// for this item.
72-
// This branch will never be taken for any normal function.
73-
// However, it's possible to `#!feature(trivial_bounds)]` to write
74-
// a function with impossible to satisfy clauses, e.g.:
75-
// `fn foo() where String: Copy {}`
76-
//
77-
// We don't usually need to worry about this kind of case,
78-
// since we would get a compilation error if the user tried
79-
// to call it. However, since we can do const propagation
80-
// even without any calls to the function, we need to make
81-
// sure that it even makes sense to try to evaluate the body.
82-
// If there are unsatisfiable where clauses, then all bets are
83-
// off, and we just give up.
84-
//
85-
// We manually filter the predicates, skipping anything that's not
86-
// "global". We are in a potentially generic context
87-
// (e.g. we are evaluating a function without substituting generic
88-
// parameters, so this filtering serves two purposes:
89-
//
90-
// 1. We skip evaluating any predicates that we would
91-
// never be able prove are unsatisfiable (e.g. `<T as Foo>`
92-
// 2. We avoid trying to normalize predicates involving generic
93-
// parameters (e.g. `<T as Foo>::MyItem`). This can confuse
94-
// the normalization code (leading to cycle errors), since
95-
// it's usually never invoked in this way.
96-
let predicates = tcx
97-
.predicates_of(def_id.to_def_id())
98-
.predicates
99-
.iter()
100-
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
101-
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
102-
trace!("ConstProp skipped for {:?}: found unsatisfiable predicates", def_id);
103-
return;
104-
}
105-
106-
trace!("ConstProp starting for {:?}", def_id);
69+
trace!("ConstPropLint starting for {:?}", def_id);
10770

10871
// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
10972
// constants, instead of just checking for const-folding succeeding.
@@ -112,7 +75,7 @@ impl<'tcx> MirLint<'tcx> for ConstProp {
11275
let mut linter = ConstPropagator::new(body, tcx);
11376
linter.visit_body(body);
11477

115-
trace!("ConstProp done for {:?}", def_id);
78+
trace!("ConstPropLint done for {:?}", def_id);
11679
}
11780
}
11881

compiler/rustc_mir_transform/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
496496
&elaborate_box_derefs::ElaborateBoxDerefs,
497497
&generator::StateTransform,
498498
&add_retag::AddRetag,
499-
&Lint(const_prop_lint::ConstProp),
499+
&Lint(const_prop_lint::ConstPropLint),
500500
];
501501
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
502502
}
@@ -554,8 +554,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
554554
&const_prop::ConstProp,
555555
&gvn::GVN,
556556
&dataflow_const_prop::DataflowConstProp,
557-
//
558-
// Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0.
559557
&const_debuginfo::ConstDebugInfo,
560558
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
561559
&early_otherwise_branch::EarlyOtherwiseBranch,

src/librustdoc/formats/cache.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,23 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
221221
_ => self.cache.stripped_mod,
222222
};
223223

224+
#[inline]
225+
fn is_from_private_dep(tcx: TyCtxt<'_>, cache: &Cache, def_id: DefId) -> bool {
226+
let krate = def_id.krate;
227+
228+
cache.masked_crates.contains(&krate) || tcx.is_private_dep(krate)
229+
}
230+
224231
// If the impl is from a masked crate or references something from a
225232
// masked crate then remove it completely.
226233
if let clean::ImplItem(ref i) = *item.kind &&
227234
(self.cache.masked_crates.contains(&item.item_id.krate())
228235
|| i.trait_
229236
.as_ref()
230-
.map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate))
237+
.map_or(false, |t| is_from_private_dep(self.tcx, self.cache, t.def_id()))
231238
|| i.for_
232239
.def_id(self.cache)
233-
.map_or(false, |d| self.cache.masked_crates.contains(&d.krate)))
240+
.map_or(false, |d| is_from_private_dep(self.tcx, self.cache, d)))
234241
{
235242
return None;
236243
}

0 commit comments

Comments
 (0)