Skip to content

Commit 2dfe414

Browse files
committed
Auto merge of rust-lang#139497 - matthiaskrgr:rollup-ptp8v5i, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#139124 (compiler: report error when trait object type param reference self) - rust-lang#139346 (Don't construct preds w escaping bound vars in `diagnostic_hir_wf_check`) - rust-lang#139379 (Use delayed bug for normalization errors in drop elaboration) - rust-lang#139421 (Fix trait upcasting to dyn type with no principal when there are projections) - rust-lang#139468 (Don't call `Span::with_parent` on the good path in `has_stashed_diagnostic`) - rust-lang#139476 (rm `RegionInferenceContext::var_infos`) - rust-lang#139490 (Update some comment/docs related to "extern intrinsic" removal) r? `@ghost` `@rustbot` modify labels: rollup
2 parents e643f59 + e41b2a4 commit 2dfe414

File tree

22 files changed

+302
-73
lines changed

22 files changed

+302
-73
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
406406
// started MIR borrowchecking with, so the region
407407
// constraints have already been taken. Use the data from
408408
// our `mbcx` instead.
409-
|vid| mbcx.regioncx.var_infos[vid].origin,
410-
|vid| mbcx.regioncx.var_infos[vid].universe,
409+
|vid| RegionVariableOrigin::Nll(mbcx.regioncx.definitions[vid].origin),
410+
|vid| mbcx.regioncx.definitions[vid].universe,
411411
)
412412
}
413413
}

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan, listify};
88
use rustc_hir::def::{CtorKind, Namespace};
99
use rustc_hir::{self as hir, CoroutineKind, LangItem};
1010
use rustc_index::IndexSlice;
11-
use rustc_infer::infer::{
12-
BoundRegionConversionTime, NllRegionVariableOrigin, RegionVariableOrigin,
13-
};
11+
use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};
1412
use rustc_infer::traits::SelectionError;
1513
use rustc_middle::bug;
1614
use rustc_middle::mir::{
@@ -633,9 +631,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
633631
) {
634632
let predicate_span = path.iter().find_map(|constraint| {
635633
let outlived = constraint.sub;
636-
if let Some(origin) = self.regioncx.var_infos.get(outlived)
637-
&& let RegionVariableOrigin::Nll(NllRegionVariableOrigin::Placeholder(_)) =
638-
origin.origin
634+
if let Some(origin) = self.regioncx.definitions.get(outlived)
635+
&& let NllRegionVariableOrigin::Placeholder(_) = origin.origin
639636
&& let ConstraintCategory::Predicate(span) = constraint.category
640637
{
641638
Some(span)

compiler/rustc_borrowck/src/polonius/dump.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ fn emit_mermaid_nll_regions<'tcx>(
334334
writeln!(out, "flowchart TD")?;
335335

336336
// Emit the region nodes.
337-
for region in regioncx.var_infos.indices() {
337+
for region in regioncx.definitions.indices() {
338338
write!(out, "{}[\"", region.as_usize())?;
339339
render_region(region, regioncx, out)?;
340340
writeln!(out, "\"]")?;
@@ -387,7 +387,7 @@ fn emit_mermaid_nll_sccs<'tcx>(
387387
// Gather and emit the SCC nodes.
388388
let mut nodes_per_scc: IndexVec<_, _> =
389389
regioncx.constraint_sccs().all_sccs().map(|_| Vec::new()).collect();
390-
for region in regioncx.var_infos.indices() {
390+
for region in regioncx.definitions.indices() {
391391
let scc = regioncx.constraint_sccs().scc(region);
392392
nodes_per_scc[scc].push(region);
393393
}

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,11 @@ impl RegionTracker {
139139
}
140140

141141
pub struct RegionInferenceContext<'tcx> {
142-
pub var_infos: VarInfos,
143-
144142
/// Contains the definition for every region variable. Region
145143
/// variables are identified by their index (`RegionVid`). The
146144
/// definition contains information about where the region came
147145
/// from as well as its final inferred value.
148-
definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
146+
pub(crate) definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
149147

150148
/// The liveness constraints added to each region. For most
151149
/// regions, these start out empty and steadily grow, though for
@@ -453,7 +451,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
453451
Rc::new(member_constraints.into_mapped(|r| constraint_sccs.scc(r)));
454452

455453
let mut result = Self {
456-
var_infos,
457454
definitions,
458455
liveness_constraints,
459456
constraints,

compiler/rustc_errors/src/lib.rs

+44-26
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ struct DiagCtxtInner {
589589
/// add more information). All stashed diagnostics must be emitted with
590590
/// `emit_stashed_diagnostics` by the time the `DiagCtxtInner` is dropped,
591591
/// otherwise an assertion failure will occur.
592-
stashed_diagnostics: FxIndexMap<(Span, StashKey), (DiagInner, Option<ErrorGuaranteed>)>,
592+
stashed_diagnostics:
593+
FxIndexMap<StashKey, FxIndexMap<Span, (DiagInner, Option<ErrorGuaranteed>)>>,
593594

594595
future_breakage_diagnostics: Vec<DiagInner>,
595596

@@ -912,8 +913,12 @@ impl<'a> DiagCtxtHandle<'a> {
912913
// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
913914
// if/when we have a more robust macro-friendly replacement for `(span, key)` as a key.
914915
// See the PR for a discussion.
915-
let key = (span.with_parent(None), key);
916-
self.inner.borrow_mut().stashed_diagnostics.insert(key, (diag, guar));
916+
self.inner
917+
.borrow_mut()
918+
.stashed_diagnostics
919+
.entry(key)
920+
.or_default()
921+
.insert(span.with_parent(None), (diag, guar));
917922

918923
guar
919924
}
@@ -922,9 +927,10 @@ impl<'a> DiagCtxtHandle<'a> {
922927
/// and [`StashKey`] as the key. Panics if the found diagnostic is an
923928
/// error.
924929
pub fn steal_non_err(self, span: Span, key: StashKey) -> Option<Diag<'a, ()>> {
925-
let key = (span.with_parent(None), key);
926930
// FIXME(#120456) - is `swap_remove` correct?
927-
let (diag, guar) = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key)?;
931+
let (diag, guar) = self.inner.borrow_mut().stashed_diagnostics.get_mut(&key).and_then(
932+
|stashed_diagnostics| stashed_diagnostics.swap_remove(&span.with_parent(None)),
933+
)?;
928934
assert!(!diag.is_error());
929935
assert!(guar.is_none());
930936
Some(Diag::new_diagnostic(self, diag))
@@ -943,9 +949,10 @@ impl<'a> DiagCtxtHandle<'a> {
943949
where
944950
F: FnMut(&mut Diag<'_>),
945951
{
946-
let key = (span.with_parent(None), key);
947952
// FIXME(#120456) - is `swap_remove` correct?
948-
let err = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key);
953+
let err = self.inner.borrow_mut().stashed_diagnostics.get_mut(&key).and_then(
954+
|stashed_diagnostics| stashed_diagnostics.swap_remove(&span.with_parent(None)),
955+
);
949956
err.map(|(err, guar)| {
950957
// The use of `::<ErrorGuaranteed>` is safe because level is `Level::Error`.
951958
assert_eq!(err.level, Error);
@@ -966,9 +973,10 @@ impl<'a> DiagCtxtHandle<'a> {
966973
key: StashKey,
967974
new_err: Diag<'_>,
968975
) -> ErrorGuaranteed {
969-
let key = (span.with_parent(None), key);
970976
// FIXME(#120456) - is `swap_remove` correct?
971-
let old_err = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key);
977+
let old_err = self.inner.borrow_mut().stashed_diagnostics.get_mut(&key).and_then(
978+
|stashed_diagnostics| stashed_diagnostics.swap_remove(&span.with_parent(None)),
979+
);
972980
match old_err {
973981
Some((old_err, guar)) => {
974982
assert_eq!(old_err.level, Error);
@@ -983,7 +991,14 @@ impl<'a> DiagCtxtHandle<'a> {
983991
}
984992

985993
pub fn has_stashed_diagnostic(&self, span: Span, key: StashKey) -> bool {
986-
self.inner.borrow().stashed_diagnostics.get(&(span.with_parent(None), key)).is_some()
994+
let inner = self.inner.borrow();
995+
if let Some(stashed_diagnostics) = inner.stashed_diagnostics.get(&key)
996+
&& !stashed_diagnostics.is_empty()
997+
{
998+
stashed_diagnostics.contains_key(&span.with_parent(None))
999+
} else {
1000+
false
1001+
}
9871002
}
9881003

9891004
/// Emit all stashed diagnostics.
@@ -997,7 +1012,11 @@ impl<'a> DiagCtxtHandle<'a> {
9971012
let inner = self.inner.borrow();
9981013
inner.err_guars.len()
9991014
+ inner.lint_err_guars.len()
1000-
+ inner.stashed_diagnostics.values().filter(|(_diag, guar)| guar.is_some()).count()
1015+
+ inner
1016+
.stashed_diagnostics
1017+
.values()
1018+
.map(|a| a.values().filter(|(_, guar)| guar.is_some()).count())
1019+
.sum::<usize>()
10011020
}
10021021

10031022
/// This excludes lint errors and delayed bugs. Unless absolutely
@@ -1486,16 +1505,18 @@ impl DiagCtxtInner {
14861505
fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
14871506
let mut guar = None;
14881507
let has_errors = !self.err_guars.is_empty();
1489-
for (_, (diag, _guar)) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
1490-
if !diag.is_error() {
1491-
// Unless they're forced, don't flush stashed warnings when
1492-
// there are errors, to avoid causing warning overload. The
1493-
// stash would've been stolen already if it were important.
1494-
if !diag.is_force_warn() && has_errors {
1495-
continue;
1508+
for (_, stashed_diagnostics) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
1509+
for (_, (diag, _guar)) in stashed_diagnostics {
1510+
if !diag.is_error() {
1511+
// Unless they're forced, don't flush stashed warnings when
1512+
// there are errors, to avoid causing warning overload. The
1513+
// stash would've been stolen already if it were important.
1514+
if !diag.is_force_warn() && has_errors {
1515+
continue;
1516+
}
14961517
}
1518+
guar = guar.or(self.emit_diagnostic(diag, None));
14971519
}
1498-
guar = guar.or(self.emit_diagnostic(diag, None));
14991520
}
15001521
guar
15011522
}
@@ -1688,6 +1709,7 @@ impl DiagCtxtInner {
16881709
if let Some((_diag, guar)) = self
16891710
.stashed_diagnostics
16901711
.values()
1712+
.flat_map(|stashed_diagnostics| stashed_diagnostics.values())
16911713
.find(|(diag, guar)| guar.is_some() && diag.is_lint.is_none())
16921714
{
16931715
*guar
@@ -1700,13 +1722,9 @@ impl DiagCtxtInner {
17001722
fn has_errors(&self) -> Option<ErrorGuaranteed> {
17011723
self.err_guars.get(0).copied().or_else(|| self.lint_err_guars.get(0).copied()).or_else(
17021724
|| {
1703-
if let Some((_diag, guar)) =
1704-
self.stashed_diagnostics.values().find(|(_diag, guar)| guar.is_some())
1705-
{
1706-
*guar
1707-
} else {
1708-
None
1709-
}
1725+
self.stashed_diagnostics.values().find_map(|stashed_diagnostics| {
1726+
stashed_diagnostics.values().find_map(|(_, guar)| *guar)
1727+
})
17101728
},
17111729
)
17121730
}

compiler/rustc_hir_analysis/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ hir_analysis_self_in_impl_self =
486486
`Self` is not valid in the self type of an impl block
487487
.note = replace `Self` with a different type
488488
489+
hir_analysis_self_in_type_alias = `Self` is not allowed in type aliases
490+
.label = `Self` is only available in impls, traits, and concrete type definitions
491+
489492
hir_analysis_self_ty_not_captured = `impl Trait` must mention the `Self` type of the trait in `use<...>`
490493
.label = `Self` type parameter is implicitly captured by this `impl Trait`
491494
.note = currently, all type parameters are required to be mentioned in the precise captures list

compiler/rustc_hir_analysis/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1707,3 +1707,11 @@ pub(crate) enum SupertraitItemShadowee {
17071707
traits: DiagSymbolList,
17081708
},
17091709
}
1710+
1711+
#[derive(Diagnostic)]
1712+
#[diag(hir_analysis_self_in_type_alias, code = E0411)]
1713+
pub(crate) struct SelfInTypeAlias {
1714+
#[primary_span]
1715+
#[label]
1716+
pub span: Span,
1717+
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use smallvec::{SmallVec, smallvec};
1616
use tracing::{debug, instrument};
1717

1818
use super::HirTyLowerer;
19+
use crate::errors::SelfInTypeAlias;
1920
use crate::hir_ty_lowering::{
2021
GenericArgCountMismatch, GenericArgCountResult, PredicateFilter, RegionInferReason,
2122
};
@@ -125,6 +126,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
125126
// ```
126127
let mut projection_bounds = FxIndexMap::default();
127128
for (proj, proj_span) in elaborated_projection_bounds {
129+
let proj = proj.map_bound(|mut b| {
130+
if let Some(term_ty) = &b.term.as_type() {
131+
let references_self = term_ty.walk().any(|arg| arg == dummy_self.into());
132+
if references_self {
133+
// With trait alias and type alias combined, type resolver
134+
// may not be able to catch all illegal `Self` usages (issue 139082)
135+
let guar = tcx.dcx().emit_err(SelfInTypeAlias { span });
136+
b.term = replace_dummy_self_with_error(tcx, b.term, guar);
137+
}
138+
}
139+
b
140+
});
141+
128142
let key = (
129143
proj.skip_binder().projection_term.def_id,
130144
tcx.anonymize_bound_vars(

compiler/rustc_hir_analysis/src/hir_wf_check.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_infer::infer::TyCtxtInferExt;
44
use rustc_infer::traits::{ObligationCause, WellFormedLoc};
55
use rustc_middle::bug;
66
use rustc_middle::query::Providers;
7-
use rustc_middle::ty::{self, TyCtxt, TypingMode, fold_regions};
7+
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt, TypingMode, fold_regions};
88
use rustc_span::def_id::LocalDefId;
99
use rustc_trait_selection::traits::{self, ObligationCtxt};
1010
use tracing::debug;
@@ -77,6 +77,15 @@ fn diagnostic_hir_wf_check<'tcx>(
7777
let tcx_ty = fold_regions(self.tcx, tcx_ty, |r, _| {
7878
if r.is_bound() { self.tcx.lifetimes.re_erased } else { r }
7979
});
80+
81+
// We may be checking the WFness of a type in an opaque with a non-lifetime bound.
82+
// Perhaps we could rebind all the escaping bound vars, but they're coming from
83+
// arbitrary debruijn indices and aren't particularly important anyways, since they
84+
// are only coming from `feature(non_lifetime_binders)` anyways.
85+
if tcx_ty.has_escaping_bound_vars() {
86+
return;
87+
}
88+
8089
let cause = traits::ObligationCause::new(
8190
ty.span,
8291
self.def_id,

compiler/rustc_mir_transform/src/elaborate_drop.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,16 @@ where
266266
let tcx = self.tcx();
267267

268268
assert_eq!(self.elaborator.typing_env().typing_mode, ty::TypingMode::PostAnalysis);
269-
// The type error for normalization may have been in dropck: see
270-
// `compute_drop_data` in rustc_borrowck, in which case we wouldn't have
271-
// deleted the MIR body and could have an error here as well.
272269
let field_ty = match tcx
273270
.try_normalize_erasing_regions(self.elaborator.typing_env(), f.ty(tcx, args))
274271
{
275272
Ok(t) => t,
276273
Err(_) => Ty::new_error(
277274
self.tcx(),
278-
self.elaborator
279-
.body()
280-
.tainted_by_errors
281-
.expect("Error in drop elaboration not found by dropck."),
275+
self.tcx().dcx().span_delayed_bug(
276+
self.elaborator.body().span,
277+
"Error normalizing in drop elaboration.",
278+
),
282279
),
283280
};
284281

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1090,26 +1090,36 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10901090
{
10911091
// See `assemble_candidates_for_unsizing` for more info.
10921092
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
1093-
let iter = data_a
1094-
.principal()
1095-
.filter(|_| {
1096-
// optionally drop the principal, if we're unsizing to no principal
1097-
data_b.principal().is_some()
1098-
})
1099-
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
1100-
.into_iter()
1101-
.chain(
1093+
let existential_predicates = if data_b.principal().is_some() {
1094+
tcx.mk_poly_existential_predicates_from_iter(
11021095
data_a
1103-
.projection_bounds()
1104-
.map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
1096+
.principal()
1097+
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
1098+
.into_iter()
1099+
.chain(
1100+
data_a
1101+
.projection_bounds()
1102+
.map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
1103+
)
1104+
.chain(
1105+
data_b
1106+
.auto_traits()
1107+
.map(ty::ExistentialPredicate::AutoTrait)
1108+
.map(ty::Binder::dummy),
1109+
),
11051110
)
1106-
.chain(
1111+
} else {
1112+
// If we're unsizing to a dyn type that has no principal, then drop
1113+
// the principal and projections from the type. We use the auto traits
1114+
// from the RHS type since as we noted that we've checked for auto
1115+
// trait compatibility during unsizing.
1116+
tcx.mk_poly_existential_predicates_from_iter(
11071117
data_b
11081118
.auto_traits()
11091119
.map(ty::ExistentialPredicate::AutoTrait)
11101120
.map(ty::Binder::dummy),
1111-
);
1112-
let existential_predicates = tcx.mk_poly_existential_predicates_from_iter(iter);
1121+
)
1122+
};
11131123
let source_trait = Ty::new_dynamic(tcx, existential_predicates, r_b, dyn_a);
11141124

11151125
// Require that the traits involved in this upcast are **equal**;

library/core/src/intrinsics/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
//!
66
//! # Const intrinsics
77
//!
8-
//! Note: any changes to the constness of intrinsics should be discussed with the language team.
9-
//! This includes changes in the stability of the constness.
10-
//!
11-
//! //FIXME(#132735) "old" style intrinsics support has been removed
12-
//! In order to make an intrinsic usable at compile-time, it needs to be declared in the "new"
13-
//! style, i.e. as a `#[rustc_intrinsic]` function, not inside an `extern` block. Then copy the
14-
//! implementation from <https://github.com/rust-lang/miri/blob/master/src/intrinsics> to
8+
//! In order to make an intrinsic unstable usable at compile-time, copy the implementation from
9+
//! <https://github.com/rust-lang/miri/blob/master/src/intrinsics> to
1510
//! <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>
16-
//! and make the intrinsic declaration a `const fn`.
11+
//! and make the intrinsic declaration below a `const fn`. This should be done in coordination with
12+
//! wg-const-eval.
1713
//!
1814
//! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
1915
//! `#[rustc_intrinsic_const_stable_indirect]` needs to be added to the intrinsic. Such a change requires

src/doc/unstable-book/src/language-features/intrinsics.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ Various intrinsics have native MIR operations that they correspond to. Instead o
5353
backends to implement both the intrinsic and the MIR operation, the `lower_intrinsics` pass
5454
will convert the calls to the MIR operation. Backends do not need to know about these intrinsics
5555
at all. These intrinsics only make sense without a body, and can be declared as a `#[rustc_intrinsic]`.
56-
The body is never used, as calls to the intrinsic do not exist anymore after MIR analyses.
56+
The body is never used as the lowering pass implements support for all backends, so we never have to
57+
use the fallback logic.
5758

5859
## Intrinsics without fallback logic
5960

0 commit comments

Comments
 (0)