Skip to content

Commit baf4abf

Browse files
committed
Auto merge of rust-lang#118107 - matthiaskrgr:rollup-k5bfkfr, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#117327 (Add documentation for some queries) - rust-lang#117835 (Note about object lifetime defaults in does not live long enough error) - rust-lang#117851 (Uplift `InferConst` to `rustc_type_ir`) - rust-lang#117973 (test: Add test for async-move in 2015 Rust proc macro) - rust-lang#117992 (Don't require intercrate mode for negative coherence) - rust-lang#118010 (Typeck break expr even if break is illegal) - rust-lang#118026 (Don't consider regions in `deref_into_dyn_supertrait` lint) - rust-lang#118089 (intercrate_ambiguity_causes: handle self ty infer + reservation impls) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8534923 + 6c62b42 commit baf4abf

File tree

54 files changed

+568
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+568
-226
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use rustc_hir as hir;
55
use rustc_hir::intravisit::Visitor;
66
use rustc_index::IndexSlice;
77
use rustc_infer::infer::NllRegionVariableOrigin;
8+
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
89
use rustc_middle::mir::{
910
Body, CallSource, CastKind, ConstraintCategory, FakeReadCause, Local, LocalInfo, Location,
1011
Operand, Place, Rvalue, Statement, StatementKind, TerminatorKind,
1112
};
1213
use rustc_middle::ty::adjustment::PointerCoercion;
13-
use rustc_middle::ty::{self, RegionVid, TyCtxt};
14+
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
1415
use rustc_span::symbol::{kw, Symbol};
1516
use rustc_span::{sym, DesugaringKind, Span};
1617
use rustc_trait_selection::traits::error_reporting::FindExprBySpan;
@@ -290,12 +291,69 @@ impl<'tcx> BorrowExplanation<'tcx> {
290291
}
291292
}
292293

294+
if let ConstraintCategory::Cast { unsize_to: Some(unsize_ty) } = category {
295+
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
296+
}
293297
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
294298
}
295299
_ => {}
296300
}
297301
}
298302

303+
fn add_object_lifetime_default_note(
304+
&self,
305+
tcx: TyCtxt<'tcx>,
306+
err: &mut Diagnostic,
307+
unsize_ty: Ty<'tcx>,
308+
) {
309+
if let ty::Adt(def, args) = unsize_ty.kind() {
310+
// We try to elaborate the object lifetime defaults and present those to the user. This should
311+
// make it clear where the region constraint is coming from.
312+
let generics = tcx.generics_of(def.did());
313+
314+
let mut has_dyn = false;
315+
let mut failed = false;
316+
317+
let elaborated_args = std::iter::zip(*args, &generics.params).map(|(arg, param)| {
318+
if let Some(ty::Dynamic(obj, _, ty::DynKind::Dyn)) = arg.as_type().map(Ty::kind) {
319+
let default = tcx.object_lifetime_default(param.def_id);
320+
321+
let re_static = tcx.lifetimes.re_static;
322+
323+
let implied_region = match default {
324+
// This is not entirely precise.
325+
ObjectLifetimeDefault::Empty => re_static,
326+
ObjectLifetimeDefault::Ambiguous => {
327+
failed = true;
328+
re_static
329+
}
330+
ObjectLifetimeDefault::Param(param_def_id) => {
331+
let index = generics.param_def_id_to_index[&param_def_id] as usize;
332+
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(|| {
333+
failed = true;
334+
re_static
335+
})
336+
}
337+
ObjectLifetimeDefault::Static => re_static,
338+
};
339+
340+
has_dyn = true;
341+
342+
Ty::new_dynamic(tcx, obj, implied_region, ty::DynKind::Dyn).into()
343+
} else {
344+
arg
345+
}
346+
});
347+
let elaborated_ty = Ty::new_adt(tcx, *def, tcx.mk_args_from_iter(elaborated_args));
348+
349+
if has_dyn && !failed {
350+
err.note(format!(
351+
"due to object lifetime defaults, `{unsize_ty}` actually means `{elaborated_ty}`"
352+
));
353+
}
354+
}
355+
}
356+
299357
fn add_lifetime_bound_suggestion_to_diagnostic(
300358
&self,
301359
err: &mut Diagnostic,

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
5353
ConstraintCategory::Yield => "yielding this value ",
5454
ConstraintCategory::UseAsConst => "using this value as a constant ",
5555
ConstraintCategory::UseAsStatic => "using this value as a static ",
56-
ConstraintCategory::Cast => "cast ",
56+
ConstraintCategory::Cast { .. } => "cast ",
5757
ConstraintCategory::CallArgument(_) => "argument ",
5858
ConstraintCategory::TypeAnnotation => "type annotation ",
5959
ConstraintCategory::ClosureBounds => "closure body ",

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19341934
*ty,
19351935
ty_fn_ptr_from,
19361936
location.to_locations(),
1937-
ConstraintCategory::Cast,
1937+
ConstraintCategory::Cast { unsize_to: None },
19381938
) {
19391939
span_mirbug!(
19401940
self,
@@ -1959,7 +1959,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19591959
*ty,
19601960
ty_fn_ptr_from,
19611961
location.to_locations(),
1962-
ConstraintCategory::Cast,
1962+
ConstraintCategory::Cast { unsize_to: None },
19631963
) {
19641964
span_mirbug!(
19651965
self,
@@ -1988,7 +1988,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19881988
*ty,
19891989
ty_fn_ptr_from,
19901990
location.to_locations(),
1991-
ConstraintCategory::Cast,
1991+
ConstraintCategory::Cast { unsize_to: None },
19921992
) {
19931993
span_mirbug!(
19941994
self,
@@ -2013,7 +2013,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20132013
self.prove_trait_ref(
20142014
trait_ref,
20152015
location.to_locations(),
2016-
ConstraintCategory::Cast,
2016+
ConstraintCategory::Cast {
2017+
unsize_to: Some(tcx.fold_regions(ty, |r, _| {
2018+
if let ty::ReVar(_) = r.kind() {
2019+
tcx.lifetimes.re_erased
2020+
} else {
2021+
r
2022+
}
2023+
})),
2024+
},
20172025
);
20182026
}
20192027

@@ -2033,7 +2041,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20332041
.iter()
20342042
.map(|predicate| predicate.with_self_ty(tcx, self_ty)),
20352043
location.to_locations(),
2036-
ConstraintCategory::Cast,
2044+
ConstraintCategory::Cast { unsize_to: None },
20372045
);
20382046

20392047
let outlives_predicate = tcx.mk_predicate(Binder::dummy(
@@ -2044,7 +2052,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20442052
self.prove_predicate(
20452053
outlives_predicate,
20462054
location.to_locations(),
2047-
ConstraintCategory::Cast,
2055+
ConstraintCategory::Cast { unsize_to: None },
20482056
);
20492057
}
20502058

@@ -2065,7 +2073,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20652073
*ty_from,
20662074
*ty_to,
20672075
location.to_locations(),
2068-
ConstraintCategory::Cast,
2076+
ConstraintCategory::Cast { unsize_to: None },
20692077
) {
20702078
span_mirbug!(
20712079
self,
@@ -2131,7 +2139,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21312139
*ty_elem,
21322140
*ty_to,
21332141
location.to_locations(),
2134-
ConstraintCategory::Cast,
2142+
ConstraintCategory::Cast { unsize_to: None },
21352143
) {
21362144
span_mirbug!(
21372145
self,

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -626,15 +626,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
626626
}
627627
};
628628

629-
let coerce_to = match opt_coerce_to {
630-
Some(c) => c,
631-
None => {
632-
// If the loop context is not a `loop { }`, then break with
633-
// a value is illegal, and `opt_coerce_to` will be `None`.
634-
// Return error in that case (#114529).
635-
return Ty::new_misc_error(tcx);
636-
}
637-
};
629+
// If the loop context is not a `loop { }`, then break with
630+
// a value is illegal, and `opt_coerce_to` will be `None`.
631+
// Set expectation to error in that case and set tainted
632+
// by error (#114529)
633+
let coerce_to = opt_coerce_to.unwrap_or_else(|| {
634+
let guar = tcx.sess.delay_span_bug(
635+
expr.span,
636+
"illegal break with value found but no error reported",
637+
);
638+
self.set_tainted_by_errors(guar);
639+
Ty::new_error(tcx, guar)
640+
});
638641

639642
// Recurse without `enclosing_breakables` borrowed.
640643
e_ty = self.check_expr_with_hint(e, coerce_to);

Diff for: compiler/rustc_infer/src/infer/at.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,15 @@ impl<'tcx> InferCtxt<'tcx> {
6565

6666
/// Forks the inference context, creating a new inference context with the same inference
6767
/// variables in the same state. This can be used to "branch off" many tests from the same
68-
/// common state. Used in coherence.
68+
/// common state.
6969
pub fn fork(&self) -> Self {
70+
self.fork_with_intercrate(self.intercrate)
71+
}
72+
73+
/// Forks the inference context, creating a new inference context with the same inference
74+
/// variables in the same state, except possibly changing the intercrate mode. This can be
75+
/// used to "branch off" many tests from the same common state. Used in negative coherence.
76+
pub fn fork_with_intercrate(&self, intercrate: bool) -> Self {
7077
Self {
7178
tcx: self.tcx,
7279
defining_use_anchor: self.defining_use_anchor,
@@ -81,7 +88,7 @@ impl<'tcx> InferCtxt<'tcx> {
8188
tainted_by_errors: self.tainted_by_errors.clone(),
8289
err_count_on_creation: self.err_count_on_creation,
8390
universe: self.universe.clone(),
84-
intercrate: self.intercrate,
91+
intercrate,
8592
next_trait_solver: self.next_trait_solver,
8693
}
8794
}

Diff for: compiler/rustc_lint/src/deref_into_dyn_supertrait.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ declare_lint! {
5050
Warn,
5151
"`Deref` implementation usage with a supertrait trait object for output might be shadowed in the future",
5252
@future_incompatible = FutureIncompatibleInfo {
53-
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
53+
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
5454
reference: "issue #89460 <https://github.com/rust-lang/rust/issues/89460>",
5555
};
5656
}
@@ -59,12 +59,13 @@ declare_lint_pass!(DerefIntoDynSupertrait => [DEREF_INTO_DYN_SUPERTRAIT]);
5959

6060
impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
6161
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
62+
let tcx = cx.tcx;
6263
// `Deref` is being implemented for `t`
6364
if let hir::ItemKind::Impl(impl_) = item.kind
6465
&& let Some(trait_) = &impl_.of_trait
65-
&& let t = cx.tcx.type_of(item.owner_id).instantiate_identity()
66+
&& let t = tcx.type_of(item.owner_id).instantiate_identity()
6667
&& let opt_did @ Some(did) = trait_.trait_def_id()
67-
&& opt_did == cx.tcx.lang_items().deref_trait()
68+
&& opt_did == tcx.lang_items().deref_trait()
6869
// `t` is `dyn t_principal`
6970
&& let ty::Dynamic(data, _, ty::Dyn) = t.kind()
7071
&& let Some(t_principal) = data.principal()
@@ -73,17 +74,22 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
7374
&& let ty::Dynamic(data, _, ty::Dyn) = target.kind()
7475
&& let Some(target_principal) = data.principal()
7576
// `target_principal` is a supertrait of `t_principal`
76-
&& supertraits(cx.tcx, t_principal.with_self_ty(cx.tcx, cx.tcx.types.trait_object_dummy_self))
77-
.any(|sup| sup.map_bound(|x| ty::ExistentialTraitRef::erase_self_ty(cx.tcx, x)) == target_principal)
77+
&& supertraits(tcx, t_principal.with_self_ty(tcx, tcx.types.trait_object_dummy_self))
78+
.any(|sup| {
79+
tcx.erase_regions(
80+
sup.map_bound(|x| ty::ExistentialTraitRef::erase_self_ty(tcx, x)),
81+
) == tcx.erase_regions(target_principal)
82+
})
7883
{
84+
let t = tcx.erase_regions(t);
7985
let label = impl_
8086
.items
8187
.iter()
8288
.find_map(|i| (i.ident.name == sym::Target).then_some(i.span))
8389
.map(|label| SupertraitAsDerefTargetLabel { label });
8490
cx.emit_spanned_lint(
8591
DEREF_INTO_DYN_SUPERTRAIT,
86-
cx.tcx.def_span(item.owner_id.def_id),
92+
tcx.def_span(item.owner_id.def_id),
8793
SupertraitAsDerefTarget { t, target_principal, label },
8894
);
8995
}

Diff for: compiler/rustc_middle/src/mir/query.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ pub enum ConstraintCategory<'tcx> {
341341
UseAsConst,
342342
UseAsStatic,
343343
TypeAnnotation,
344-
Cast,
344+
Cast {
345+
/// Whether this is an unsizing cast and if yes, this contains the target type.
346+
/// Region variables are erased to ReErased.
347+
unsize_to: Option<Ty<'tcx>>,
348+
},
345349

346350
/// A constraint that came from checking the body of a closure.
347351
///

Diff for: compiler/rustc_middle/src/query/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ pub use plumbing::{IntoQueryParam, TyCtxtAt, TyCtxtEnsure, TyCtxtEnsureWithValue
109109
// Queries marked with `fatal_cycle` do not need the latter implementation,
110110
// as they will raise an fatal error on query cycles instead.
111111
rustc_queries! {
112+
/// This exists purely for testing the interactions between delay_span_bug and incremental.
112113
query trigger_delay_span_bug(key: DefId) -> () {
113-
desc { "triggering a delay span bug" }
114+
desc { "triggering a delay span bug for testing incremental" }
114115
}
115116

117+
/// Collects the list of all tools registered using `#![register_tool]`.
116118
query registered_tools(_: ()) -> &'tcx ty::RegisteredTools {
117119
arena_cache
118120
desc { "compute registered tools for crate" }
@@ -286,6 +288,7 @@ rustc_queries! {
286288
}
287289
}
288290

291+
/// The root query triggering all analysis passes like typeck or borrowck.
289292
query analysis(key: ()) -> Result<(), ErrorGuaranteed> {
290293
eval_always
291294
desc { "running analysis passes on this crate" }
@@ -1778,10 +1781,17 @@ rustc_queries! {
17781781
desc { "calculating the missing lang items in a crate" }
17791782
separate_provide_extern
17801783
}
1784+
1785+
/// The visible parent map is a map from every item to a visible parent.
1786+
/// It prefers the shortest visible path to an item.
1787+
/// Used for diagnostics, for example path trimming.
1788+
/// The parents are modules, enums or traits.
17811789
query visible_parent_map(_: ()) -> &'tcx DefIdMap<DefId> {
17821790
arena_cache
17831791
desc { "calculating the visible parent map" }
17841792
}
1793+
/// Collects the "trimmed", shortest accessible paths to all items for diagnostics.
1794+
/// See the [provider docs](`rustc_middle::ty::print::trimmed_def_paths`) for more info.
17851795
query trimmed_def_paths(_: ()) -> &'tcx FxHashMap<DefId, Symbol> {
17861796
arena_cache
17871797
desc { "calculating trimmed def paths" }

Diff for: compiler/rustc_middle/src/ty/consts/kind.rs

-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::mir;
33
use crate::ty::abstract_const::CastKind;
44
use crate::ty::GenericArgsRef;
55
use crate::ty::{self, visit::TypeVisitableExt as _, List, Ty, TyCtxt};
6-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
76
use rustc_hir::def_id::DefId;
87
use rustc_macros::HashStable;
98

@@ -77,28 +76,3 @@ static_assert_size!(Expr<'_>, 24);
7776

7877
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
7978
static_assert_size!(super::ConstKind<'_>, 32);
80-
81-
/// An inference variable for a const, for use in const generics.
82-
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
83-
pub enum InferConst {
84-
/// Infer the value of the const.
85-
Var(ty::ConstVid),
86-
/// Infer the value of the effect.
87-
///
88-
/// For why this is separate from the `Var` variant above, see the
89-
/// documentation on `EffectVid`.
90-
EffectVar(ty::EffectVid),
91-
/// A fresh const variable. See `infer::freshen` for more details.
92-
Fresh(u32),
93-
}
94-
95-
impl<CTX> HashStable<CTX> for InferConst {
96-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
97-
match self {
98-
InferConst::Var(_) | InferConst::EffectVar(_) => {
99-
panic!("const variables should not be hashed: {self:?}")
100-
}
101-
InferConst::Fresh(i) => i.hash_stable(hcx, hasher),
102-
}
103-
}
104-
}

Diff for: compiler/rustc_middle/src/ty/context.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use crate::traits::solve::{
2626
};
2727
use crate::ty::{
2828
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
29-
ImplPolarity, InferTy, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig,
30-
Predicate, PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind,
31-
TyVid, TypeAndMut, Visibility,
29+
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
30+
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
31+
TypeAndMut, Visibility,
3232
};
3333
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3434
use rustc_ast::{self as ast, attr};
@@ -95,15 +95,13 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9595
type ParamTy = ParamTy;
9696
type BoundTy = ty::BoundTy;
9797
type PlaceholderTy = ty::PlaceholderType;
98-
type InferTy = InferTy;
9998

10099
type ErrorGuaranteed = ErrorGuaranteed;
101100
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
102101
type PolyFnSig = PolyFnSig<'tcx>;
103102
type AllocId = crate::mir::interpret::AllocId;
104103

105104
type Const = ty::Const<'tcx>;
106-
type InferConst = ty::InferConst;
107105
type AliasConst = ty::UnevaluatedConst<'tcx>;
108106
type PlaceholderConst = ty::PlaceholderConst;
109107
type ParamConst = ty::ParamConst;

0 commit comments

Comments
 (0)