Skip to content

Commit cc5de07

Browse files
authored
Merge pull request rust-lang#5082 from RalfJung/win-metadata
windows shims: avoid returning possibly outdated metadata
2 parents 7b0778b + 19a1abe commit cc5de07

480 files changed

Lines changed: 7896 additions & 6778 deletions

File tree

Some content is hidden

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

compiler/rustc_abi/src/layout.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::BTreeSet;
22
use std::fmt::{self, Write};
33
use std::ops::Deref;
4+
use std::range::RangeInclusive;
45
use std::{cmp, iter};
56

67
use rustc_hashes::Hash64;
@@ -631,11 +632,13 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
631632
let all_indices = variants.indices();
632633
let needs_disc =
633634
|index: VariantIdx| index != largest_variant_index && !absent(&variants[index]);
634-
let niche_variants = all_indices.clone().find(|v| needs_disc(*v)).unwrap()
635-
..=all_indices.rev().find(|v| needs_disc(*v)).unwrap();
635+
let niche_variants = RangeInclusive {
636+
start: all_indices.clone().find(|v| needs_disc(*v)).unwrap(),
637+
last: all_indices.rev().find(|v| needs_disc(*v)).unwrap(),
638+
};
636639

637640
let count =
638-
(niche_variants.end().index() as u128 - niche_variants.start().index() as u128) + 1;
641+
(niche_variants.last.index() as u128 - niche_variants.start.index() as u128) + 1;
639642

640643
// Use the largest niche in the largest variant.
641644
let niche = variant_layouts[largest_variant_index].largest_niche?;

compiler/rustc_abi/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ use std::fmt;
4040
#[cfg(feature = "nightly")]
4141
use std::iter::Step;
4242
use std::num::{NonZeroUsize, ParseIntError};
43-
use std::ops::{Add, AddAssign, Deref, Mul, RangeFull, RangeInclusive, Sub};
43+
use std::ops::{Add, AddAssign, Deref, Mul, RangeFull, Sub};
44+
use std::range::RangeInclusive;
4445
use std::str::FromStr;
4546

4647
use bitflags::bitflags;
@@ -1958,7 +1959,7 @@ pub enum Variants<FieldIdx: Idx, VariantIdx: Idx> {
19581959
}
19591960

19601961
// NOTE: This struct is generic over the VariantIdx for rust-analyzer usage.
1961-
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
1962+
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
19621963
#[cfg_attr(feature = "nightly", derive(StableHash))]
19631964
pub enum TagEncoding<VariantIdx: Idx> {
19641965
/// The tag directly stores the discriminant, but possibly with a smaller layout

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
124124
// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
125125
let sig_id = if let Some(delegation_info) = self.resolver.delegation_info(self.owner.def_id)
126126
{
127-
self.get_sig_id(delegation_info.resolution_node, span)
127+
self.get_sig_id(delegation_info.resolution_id, span)
128128
} else {
129129
self.dcx().span_delayed_bug(
130130
span,
@@ -230,22 +230,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
230230
.collect::<Vec<_>>()
231231
}
232232

233-
fn get_sig_id(&self, mut node_id: NodeId, span: Span) -> Result<DefId, ErrorGuaranteed> {
234-
let mut visited: FxHashSet<NodeId> = Default::default();
233+
fn get_sig_id(&self, mut def_id: DefId, span: Span) -> Result<DefId, ErrorGuaranteed> {
234+
let mut visited: FxHashSet<DefId> = Default::default();
235235
let mut path: SmallVec<[DefId; 1]> = Default::default();
236236

237237
loop {
238-
visited.insert(node_id);
239-
240-
let Some(def_id) = self.get_resolution_id(node_id) else {
241-
return Err(self.tcx.dcx().span_delayed_bug(
242-
span,
243-
format!(
244-
"LoweringContext: couldn't resolve node {:?} in delegation item",
245-
node_id
246-
),
247-
));
248-
};
238+
visited.insert(def_id);
249239

250240
path.push(def_id);
251241

@@ -255,8 +245,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
255245
if let Some(local_id) = def_id.as_local()
256246
&& let Some(delegation_info) = self.resolver.delegation_info(local_id)
257247
{
258-
node_id = delegation_info.resolution_node;
259-
if visited.contains(&node_id) {
248+
def_id = delegation_info.resolution_id;
249+
if visited.contains(&def_id) {
260250
// We encountered a cycle in the resolution, or delegation callee refers to non-existent
261251
// entity, in this case emit an error.
262252
return Err(match visited.len() {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15491549
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
15501550
let target_id = match destination {
15511551
Some((id, _)) => {
1552-
if let Some(loop_id) = self.resolver.get_label_res(id) {
1552+
if let Some(loop_id) = self.owner.get_label_res(id) {
15531553
let local_id = self.ident_and_label_to_local_id[&loop_id];
15541554
let loop_hir_id = HirId { owner: self.current_hir_id_owner, local_id };
15551555
Ok(loop_hir_id)

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,50 +1192,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
11921192
})
11931193
}
11941194

1195-
fn resolve_pin_drop_sugar_impl_item(
1195+
fn check_pin_drop_sugar_impl_item(
11961196
&self,
11971197
i: &AssocItem,
11981198
ident: Ident,
1199-
span: Span,
1200-
) -> (Ident, Result<DefId, ErrorGuaranteed>) {
1201-
let trait_item_def_id = self
1202-
.get_partial_res(i.id)
1203-
.and_then(|r| r.expect_full_res().opt_def_id())
1204-
.ok_or_else(|| {
1205-
self.dcx().span_delayed_bug(span, "could not resolve trait item being implemented")
1206-
});
1207-
1208-
let is_pin_drop_sugar = match &i.kind {
1209-
AssocItemKind::Fn(fn_kind) => fn_kind.is_pin_drop_sugar(),
1210-
_ => false,
1211-
};
1212-
let def_id = match trait_item_def_id {
1213-
Ok(def_id) => def_id,
1214-
Err(guar) => return (ident, Err(guar)),
1215-
};
1216-
if !is_pin_drop_sugar {
1217-
return (ident, Ok(def_id));
1218-
}
1219-
1220-
let is_drop_pin_drop = self
1221-
.tcx
1222-
.lang_items()
1223-
.drop_trait()
1224-
.is_some_and(|drop_trait| self.tcx.parent(def_id) == drop_trait);
1225-
if is_drop_pin_drop {
1226-
// Associated item collection still derives the impl item's name from HIR.
1227-
return (Ident::new(sym::pin_drop, ident.span), Ok(def_id));
1199+
trait_item: Result<DefId, ErrorGuaranteed>,
1200+
) -> Ident {
1201+
if let AssocItemKind::Fn(fn_kind) = &i.kind
1202+
&& fn_kind.is_pin_drop_sugar()
1203+
{
1204+
if let Ok(trait_item) = trait_item
1205+
&& self
1206+
.tcx
1207+
.lang_items()
1208+
.drop_trait()
1209+
.is_none_or(|drop_trait| self.tcx.parent(trait_item) != drop_trait)
1210+
{
1211+
self.dcx()
1212+
.struct_span_err(
1213+
i.span,
1214+
"method `drop` with `&pin mut self` is only supported for the `Drop` trait",
1215+
)
1216+
.with_span_label(i.span, "not a `Drop::pin_drop` implementation")
1217+
.emit();
1218+
}
1219+
return Ident::new(sym::pin_drop, ident.span);
12281220
}
12291221

1230-
let guar = self
1231-
.dcx()
1232-
.struct_span_err(
1233-
i.span,
1234-
"method `drop` with `&pin mut self` is only supported for the `Drop` trait",
1235-
)
1236-
.with_span_label(i.span, "not a `Drop::pin_drop` implementation")
1237-
.emit();
1238-
(ident, Err(guar))
1222+
ident
12391223
}
12401224

12411225
fn lower_impl_item(
@@ -1356,8 +1340,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
13561340

13571341
let span = self.lower_span(i.span);
13581342
let (effective_ident, impl_kind) = if is_in_trait_impl {
1359-
let (effective_ident, trait_item_def_id) =
1360-
self.resolve_pin_drop_sugar_impl_item(i, ident, span);
1343+
let trait_item_def_id = self
1344+
.get_partial_res(i.id)
1345+
.and_then(|r| r.expect_full_res().opt_def_id())
1346+
.ok_or_else(|| {
1347+
self.dcx()
1348+
.span_delayed_bug(span, "could not resolve trait item being implemented")
1349+
});
1350+
let effective_ident = self.check_pin_drop_sugar_impl_item(i, ident, trait_item_def_id);
13611351
(effective_ident, ImplItemImplKind::Trait { defaultness, trait_item_def_id })
13621352
} else {
13631353
(ident, ImplItemImplKind::Inherent { vis_span: self.lower_span(i.vis.span) })
@@ -1928,11 +1918,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
19281918

19291919
// Introduce extra lifetimes if late resolution tells us to.
19301920
let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
1931-
params.extend(extra_lifetimes.into_iter().filter_map(|&(ident, node_id, res)| {
1921+
params.extend(extra_lifetimes.into_iter().map(|&(ident, node_id, kind)| {
19321922
self.lifetime_res_to_generic_param(
19331923
ident,
19341924
node_id,
1935-
res,
1925+
kind,
19361926
hir::GenericParamSource::Generics,
19371927
)
19381928
}));

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use rustc_hir::definitions::PerParentDisambiguatorState;
5555
use rustc_hir::lints::DelayedLint;
5656
use rustc_hir::{
5757
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
58-
LifetimeSyntax, ParamName, Target, TraitCandidate, find_attr,
58+
LifetimeSyntax, MissingLifetimeKind, ParamName, Target, TraitCandidate, find_attr,
5959
};
6060
use rustc_index::{Idx, IndexSlice, IndexVec};
6161
use rustc_macros::extension;
@@ -293,24 +293,14 @@ impl<'tcx> ResolverAstLowering<'tcx> {
293293
self.import_res_map.get(&id).copied().unwrap_or_default()
294294
}
295295

296-
/// Obtains resolution for a label with the given `NodeId`.
297-
fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
298-
self.label_res_map.get(&id).copied()
299-
}
300-
301-
/// Obtains resolution for a lifetime with the given `NodeId`.
302-
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
303-
self.lifetimes_res_map.get(&id).copied()
304-
}
305-
306296
/// Obtain the list of lifetimes parameters to add to an item.
307297
///
308298
/// Extra lifetime parameters should only be added in places that can appear
309299
/// as a `binder` in `LifetimeRes`.
310300
///
311301
/// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
312302
/// should appear at the enclosing `PolyTraitRef`.
313-
fn extra_lifetime_params(&self, id: NodeId) -> &[(Ident, NodeId, LifetimeRes)] {
303+
fn extra_lifetime_params(&self, id: NodeId) -> &[(Ident, NodeId, MissingLifetimeKind)] {
314304
self.extra_lifetime_params_map.get(&id).map_or(&[], |v| &v[..])
315305
}
316306

@@ -321,10 +311,6 @@ impl<'tcx> ResolverAstLowering<'tcx> {
321311
fn owner_def_id(&self, id: NodeId) -> LocalDefId {
322312
self.owners[&id].def_id
323313
}
324-
325-
fn lifetime_elision_allowed(&self, id: NodeId) -> bool {
326-
self.lifetime_elision_allowed.contains(&id)
327-
}
328314
}
329315

330316
/// How relaxed bounds `?Trait` should be treated.
@@ -542,7 +528,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
542528
let ast_index = index_crate(&resolver, &krate);
543529
let mut owners = IndexVec::from_fn_n(
544530
|_| hir::MaybeOwner::Phantom,
545-
tcx.definitions_untracked().def_index_count(),
531+
tcx.definitions_untracked().num_definitions(),
546532
);
547533

548534
let mut lowerer = item::ItemLowerer {
@@ -948,43 +934,30 @@ impl<'hir> LoweringContext<'_, 'hir> {
948934
&mut self,
949935
ident: Ident,
950936
node_id: NodeId,
951-
res: LifetimeRes,
937+
kind: MissingLifetimeKind,
952938
source: hir::GenericParamSource,
953-
) -> Option<hir::GenericParam<'hir>> {
954-
let (name, kind) = match res {
955-
LifetimeRes::Param { .. } => {
956-
(hir::ParamName::Plain(ident), hir::LifetimeParamKind::Explicit)
957-
}
958-
LifetimeRes::Fresh { param, kind, .. } => {
959-
// Late resolution delegates to us the creation of the `LocalDefId`.
960-
let _def_id = self.create_def(
961-
param,
962-
Some(kw::UnderscoreLifetime),
963-
DefKind::LifetimeParam,
964-
ident.span,
965-
);
966-
debug!(?_def_id);
939+
) -> hir::GenericParam<'hir> {
940+
// Late resolution delegates to us the creation of the `LocalDefId`.
941+
let _def_id = self.create_def(
942+
node_id,
943+
Some(kw::UnderscoreLifetime),
944+
DefKind::LifetimeParam,
945+
ident.span,
946+
);
947+
debug!(?_def_id);
967948

968-
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided(kind))
969-
}
970-
LifetimeRes::Static { .. } | LifetimeRes::Error(..) => return None,
971-
res => panic!(
972-
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
973-
res, ident, ident.span
974-
),
975-
};
976949
let hir_id = self.lower_node_id(node_id);
977950
let def_id = self.local_def_id(node_id);
978-
Some(hir::GenericParam {
951+
hir::GenericParam {
979952
hir_id,
980953
def_id,
981-
name,
954+
name: hir::ParamName::Fresh,
982955
span: self.lower_span(ident.span),
983956
pure_wrt_drop: false,
984-
kind: hir::GenericParamKind::Lifetime { kind },
957+
kind: hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Elided(kind) },
985958
colon_span: None,
986959
source,
987-
})
960+
}
988961
}
989962

990963
/// Lowers a lifetime binder that defines `generic_params`, returning the corresponding HIR
@@ -1005,7 +978,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1005978
debug!(?extra_lifetimes);
1006979
let extra_lifetimes: Vec<_> = extra_lifetimes
1007980
.iter()
1008-
.filter_map(|&(ident, node_id, res)| {
981+
.map(|&(ident, node_id, res)| {
1009982
self.lifetime_res_to_generic_param(
1010983
ident,
1011984
node_id,
@@ -1625,7 +1598,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16251598

16261599
None => {
16271600
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
1628-
self.resolver.get_lifetime_res(t.id)
1601+
self.owner.get_lifetime_res(t.id)
16291602
{
16301603
assert_eq!(start.plus(1), end);
16311604
start
@@ -1866,7 +1839,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
18661839
_ => hir::ImplicitSelfKind::None,
18671840
}
18681841
}))
1869-
.set_lifetime_elision_allowed(self.resolver.lifetime_elision_allowed(fn_node_id))
1842+
.set_lifetime_elision_allowed(
1843+
self.owner.id == fn_node_id && self.owner.lifetime_elision_allowed,
1844+
)
18701845
.set_c_variadic(c_variadic);
18711846

18721847
self.arena.alloc(hir::FnDecl { inputs, output, fn_decl_kind })
@@ -2032,7 +2007,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20322007
source: LifetimeSource,
20332008
syntax: LifetimeSyntax,
20342009
) -> &'hir hir::Lifetime {
2035-
let res = if let Some(res) = self.resolver.get_lifetime_res(id) {
2010+
let res = if let Some(res) = self.owner.get_lifetime_res(id) {
20362011
match res {
20372012
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
20382013
LifetimeRes::Fresh { param, .. } => {
@@ -2118,13 +2093,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
21182093
// AST resolution emitted an error on those parameters, so we lower them using
21192094
// `ParamName::Error`.
21202095
let ident = self.lower_ident(param.ident);
2121-
let param_name = if let Some(LifetimeRes::Error(..)) =
2122-
self.resolver.get_lifetime_res(param.id)
2123-
{
2124-
ParamName::Error(ident)
2125-
} else {
2126-
ParamName::Plain(ident)
2127-
};
2096+
let param_name =
2097+
if let Some(LifetimeRes::Error(..)) = self.owner.get_lifetime_res(param.id) {
2098+
ParamName::Error(ident)
2099+
} else {
2100+
ParamName::Plain(ident)
2101+
};
21282102
let kind =
21292103
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
21302104

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::errors::{
1717
};
1818
use super::{
1919
AllowReturnTypeNotation, GenericArgsCtor, GenericArgsMode, ImplTraitContext, ImplTraitPosition,
20-
LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
20+
LifetimeRes, LoweringContext, ParamMode,
2121
};
2222

2323
impl<'hir> LoweringContext<'_, 'hir> {
@@ -422,7 +422,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
422422
segment_ident_span: Span,
423423
generic_args: &mut GenericArgsCtor<'hir>,
424424
) {
425-
let (start, end) = match self.resolver.get_lifetime_res(segment_id) {
425+
let (start, end) = match self.owner.get_lifetime_res(segment_id) {
426426
Some(LifetimeRes::ElidedAnchor { start, end }) => (start, end),
427427
None => return,
428428
Some(res) => {

0 commit comments

Comments
 (0)