Skip to content

Commit 5983a3a

Browse files
committed
Auto merge of #108586 - matthiaskrgr:rollup-ry9u2ou, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #108297 (Exit when there are unmatched delims to avoid noisy diagnostics) - #108531 (rustdoc: Show that repeated expression arrays can be made with constant values) - #108536 (Update books) - #108550 (Remove the `capture_disjoint_fields` feature) - #108551 (Descriptive error when users try to combine RPITIT/AFIT with specialization) - #108554 (Only look for param in item's generics if it actually comes from generics) - #108555 (Fix a race in the query system) - #108558 (add missing feature in core/tests) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents ca1bc7f + 3abc41a commit 5983a3a

File tree

134 files changed

+554
-1733
lines changed

Some content is hidden

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

134 files changed

+554
-1733
lines changed

compiler/rustc_ast_lowering/src/item.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1339,13 +1339,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
13391339
.map(|predicate| self.lower_where_predicate(predicate)),
13401340
);
13411341

1342-
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> =
1343-
self.lower_generic_params_mut(&generics.params).collect();
1342+
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
1343+
.lower_generic_params_mut(&generics.params, hir::GenericParamSource::Generics)
1344+
.collect();
13441345

13451346
// Introduce extra lifetimes if late resolution tells us to.
13461347
let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id);
13471348
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
1348-
self.lifetime_res_to_generic_param(ident, node_id, res)
1349+
self.lifetime_res_to_generic_param(
1350+
ident,
1351+
node_id,
1352+
res,
1353+
hir::GenericParamSource::Generics,
1354+
)
13491355
}));
13501356

13511357
let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
@@ -1449,7 +1455,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
14491455
span,
14501456
}) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
14511457
hir_id: self.next_id(),
1452-
bound_generic_params: self.lower_generic_params(bound_generic_params),
1458+
bound_generic_params: self
1459+
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
14531460
bounded_ty: self
14541461
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
14551462
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {

compiler/rustc_ast_lowering/src/lib.rs

+30-9
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
804804
ident: Ident,
805805
node_id: NodeId,
806806
res: LifetimeRes,
807+
source: hir::GenericParamSource,
807808
) -> Option<hir::GenericParam<'hir>> {
808809
let (name, kind) = match res {
809810
LifetimeRes::Param { .. } => {
@@ -837,6 +838,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
837838
pure_wrt_drop: false,
838839
kind: hir::GenericParamKind::Lifetime { kind },
839840
colon_span: None,
841+
source,
840842
})
841843
}
842844

@@ -852,11 +854,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
852854
binder: NodeId,
853855
generic_params: &[GenericParam],
854856
) -> &'hir [hir::GenericParam<'hir>] {
855-
let mut generic_params: Vec<_> = self.lower_generic_params_mut(generic_params).collect();
857+
let mut generic_params: Vec<_> = self
858+
.lower_generic_params_mut(generic_params, hir::GenericParamSource::Binder)
859+
.collect();
856860
let extra_lifetimes = self.resolver.take_extra_lifetime_params(binder);
857861
debug!(?extra_lifetimes);
858862
generic_params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
859-
self.lifetime_res_to_generic_param(ident, node_id, res)
863+
self.lifetime_res_to_generic_param(ident, node_id, res, hir::GenericParamSource::Binder)
860864
}));
861865
let generic_params = self.arena.alloc_from_iter(generic_params);
862866
debug!(?generic_params);
@@ -1375,8 +1379,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13751379
span,
13761380
);
13771381
let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
1378-
let (param, bounds, path) =
1379-
self.lower_generic_and_bounds(*def_node_id, span, ident, bounds);
1382+
let (param, bounds, path) = self.lower_universal_param_and_bounds(
1383+
*def_node_id,
1384+
span,
1385+
ident,
1386+
bounds,
1387+
);
13801388
self.impl_trait_defs.push(param);
13811389
if let Some(bounds) = bounds {
13821390
self.impl_trait_bounds.push(bounds);
@@ -1530,6 +1538,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15301538
pure_wrt_drop: false,
15311539
kind: hir::GenericParamKind::Lifetime { kind },
15321540
colon_span: None,
1541+
source: hir::GenericParamSource::Generics,
15331542
}
15341543
},
15351544
));
@@ -1987,6 +1996,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19871996
pure_wrt_drop: false,
19881997
kind: hir::GenericParamKind::Lifetime { kind },
19891998
colon_span: None,
1999+
source: hir::GenericParamSource::Generics,
19902000
}
19912001
},
19922002
));
@@ -2152,16 +2162,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21522162
fn lower_generic_params_mut<'s>(
21532163
&'s mut self,
21542164
params: &'s [GenericParam],
2165+
source: hir::GenericParamSource,
21552166
) -> impl Iterator<Item = hir::GenericParam<'hir>> + Captures<'a> + Captures<'s> {
2156-
params.iter().map(move |param| self.lower_generic_param(param))
2167+
params.iter().map(move |param| self.lower_generic_param(param, source))
21572168
}
21582169

2159-
fn lower_generic_params(&mut self, params: &[GenericParam]) -> &'hir [hir::GenericParam<'hir>] {
2160-
self.arena.alloc_from_iter(self.lower_generic_params_mut(params))
2170+
fn lower_generic_params(
2171+
&mut self,
2172+
params: &[GenericParam],
2173+
source: hir::GenericParamSource,
2174+
) -> &'hir [hir::GenericParam<'hir>] {
2175+
self.arena.alloc_from_iter(self.lower_generic_params_mut(params, source))
21612176
}
21622177

21632178
#[instrument(level = "trace", skip(self))]
2164-
fn lower_generic_param(&mut self, param: &GenericParam) -> hir::GenericParam<'hir> {
2179+
fn lower_generic_param(
2180+
&mut self,
2181+
param: &GenericParam,
2182+
source: hir::GenericParamSource,
2183+
) -> hir::GenericParam<'hir> {
21652184
let (name, kind) = self.lower_generic_param_kind(param);
21662185

21672186
let hir_id = self.lower_node_id(param.id);
@@ -2174,6 +2193,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21742193
pure_wrt_drop: self.tcx.sess.contains_name(&param.attrs, sym::may_dangle),
21752194
kind,
21762195
colon_span: param.colon_span.map(|s| self.lower_span(s)),
2196+
source,
21772197
}
21782198
}
21792199

@@ -2266,7 +2286,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22662286
}
22672287

22682288
#[instrument(level = "debug", skip(self), ret)]
2269-
fn lower_generic_and_bounds(
2289+
fn lower_universal_param_and_bounds(
22702290
&mut self,
22712291
node_id: NodeId,
22722292
span: Span,
@@ -2286,6 +2306,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22862306
span,
22872307
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
22882308
colon_span: None,
2309+
source: hir::GenericParamSource::Generics,
22892310
};
22902311

22912312
let preds = self.lower_generic_bound_predicate(

compiler/rustc_expand/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub(crate) fn string_to_stream(source_str: String) -> TokenStream {
4343
ps.source_map().new_source_file(PathBuf::from("bogofile").into(), source_str),
4444
None,
4545
)
46-
.0
4746
}
4847

4948
/// Parses a string, returns a crate.

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,6 @@ declare_features! (
316316
(active, c_unwind, "1.52.0", Some(74990), None),
317317
/// Allows using C-variadics.
318318
(active, c_variadic, "1.34.0", Some(44930), None),
319-
/// Allows capturing disjoint fields in a closure/generator (RFC 2229).
320-
(incomplete, capture_disjoint_fields, "1.49.0", Some(53488), None),
321319
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
322320
(active, cfg_sanitize, "1.41.0", Some(39699), None),
323321
/// Allows `cfg(target_abi = "...")`.

compiler/rustc_feature/src/removed.rs

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ declare_features! (
5252
(removed, allow_fail, "1.19.0", Some(46488), None, Some("removed due to no clear use cases")),
5353
(removed, await_macro, "1.38.0", Some(50547), None,
5454
Some("subsumed by `.await` syntax")),
55+
/// Allows capturing disjoint fields in a closure/generator (RFC 2229).
56+
(removed, capture_disjoint_fields, "1.49.0", Some(53488), None, Some("stabilized in Rust 2021")),
5557
/// Allows comparing raw pointers during const eval.
5658
(removed, const_compare_raw_pointers, "1.46.0", Some(53020), None,
5759
Some("cannot be allowed in const eval in any meaningful way")),

compiler/rustc_hir/src/hir.rs

+15
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ pub struct GenericParam<'hir> {
498498
pub pure_wrt_drop: bool,
499499
pub kind: GenericParamKind<'hir>,
500500
pub colon_span: Option<Span>,
501+
pub source: GenericParamSource,
501502
}
502503

503504
impl<'hir> GenericParam<'hir> {
@@ -516,6 +517,20 @@ impl<'hir> GenericParam<'hir> {
516517
}
517518
}
518519

520+
/// Records where the generic parameter originated from.
521+
///
522+
/// This can either be from an item's generics, in which case it's typically
523+
/// early-bound (but can be a late-bound lifetime in functions, for example),
524+
/// or from a `for<...>` binder, in which case it's late-bound (and notably,
525+
/// does not show up in the parent item's generics).
526+
#[derive(Debug, HashStable_Generic, PartialEq, Eq, Copy, Clone)]
527+
pub enum GenericParamSource {
528+
// Early or late-bound parameters defined on an item
529+
Generics,
530+
// Late-bound parameters defined via a `for<...>`
531+
Binder,
532+
}
533+
519534
#[derive(Default)]
520535
pub struct GenericParamCount {
521536
pub lifetimes: usize,

compiler/rustc_hir_analysis/src/check/check.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -792,17 +792,19 @@ fn check_impl_items_against_trait<'tcx>(
792792
trait_def.must_implement_one_of.as_deref();
793793

794794
for &trait_item_id in tcx.associated_item_def_ids(impl_trait_ref.def_id) {
795-
let is_implemented = ancestors
796-
.leaf_def(tcx, trait_item_id)
795+
let leaf_def = ancestors.leaf_def(tcx, trait_item_id);
796+
797+
let is_implemented = leaf_def
798+
.as_ref()
797799
.map_or(false, |node_item| node_item.item.defaultness(tcx).has_value());
798800

799801
if !is_implemented && tcx.impl_defaultness(impl_id).is_final() {
800802
missing_items.push(tcx.associated_item(trait_item_id));
801803
}
802804

803805
// true if this item is specifically implemented in this impl
804-
let is_implemented_here = ancestors
805-
.leaf_def(tcx, trait_item_id)
806+
let is_implemented_here = leaf_def
807+
.as_ref()
806808
.map_or(false, |node_item| !node_item.defining_node.is_from_trait());
807809

808810
if !is_implemented_here {
@@ -831,6 +833,36 @@ fn check_impl_items_against_trait<'tcx>(
831833
}
832834
}
833835
}
836+
837+
if let Some(leaf_def) = &leaf_def
838+
&& !leaf_def.is_final()
839+
&& let def_id = leaf_def.item.def_id
840+
&& tcx.impl_method_has_trait_impl_trait_tys(def_id)
841+
{
842+
let def_kind = tcx.def_kind(def_id);
843+
let descr = tcx.def_kind_descr(def_kind, def_id);
844+
let (msg, feature) = if tcx.asyncness(def_id).is_async() {
845+
(
846+
format!("async {descr} in trait cannot be specialized"),
847+
sym::async_fn_in_trait,
848+
)
849+
} else {
850+
(
851+
format!(
852+
"{descr} with return-position `impl Trait` in trait cannot be specialized"
853+
),
854+
sym::return_position_impl_trait_in_trait,
855+
)
856+
};
857+
tcx.sess
858+
.struct_span_err(tcx.def_span(def_id), msg)
859+
.note(format!(
860+
"specialization behaves in inconsistent and \
861+
surprising ways with `#![feature({feature})]`, \
862+
and for now is disallowed"
863+
))
864+
.emit();
865+
}
834866
}
835867

836868
if !missing_items.is_empty() {

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+45-36
Original file line numberDiff line numberDiff line change
@@ -1034,45 +1034,53 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
10341034
fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: DefId) -> ObjectLifetimeDefault {
10351035
debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam);
10361036
let param_def_id = param_def_id.expect_local();
1037-
let parent_def_id = tcx.local_parent(param_def_id);
1038-
let generics = tcx.hir().get_generics(parent_def_id).unwrap();
1039-
let param_hir_id = tcx.local_def_id_to_hir_id(param_def_id);
1040-
let param = generics.params.iter().find(|p| p.hir_id == param_hir_id).unwrap();
1041-
1042-
// Scan the bounds and where-clauses on parameters to extract bounds
1043-
// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
1044-
// for each type parameter.
1045-
match param.kind {
1046-
GenericParamKind::Type { .. } => {
1047-
let mut set = Set1::Empty;
1048-
1049-
// Look for `type: ...` where clauses.
1050-
for bound in generics.bounds_for_param(param_def_id) {
1051-
// Ignore `for<'a> type: ...` as they can change what
1052-
// lifetimes mean (although we could "just" handle it).
1053-
if !bound.bound_generic_params.is_empty() {
1054-
continue;
1055-
}
1037+
let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(param_def_id) else {
1038+
bug!("expected GenericParam for object_lifetime_default");
1039+
};
1040+
match param.source {
1041+
hir::GenericParamSource::Generics => {
1042+
let parent_def_id = tcx.local_parent(param_def_id);
1043+
let generics = tcx.hir().get_generics(parent_def_id).unwrap();
1044+
let param_hir_id = tcx.local_def_id_to_hir_id(param_def_id);
1045+
let param = generics.params.iter().find(|p| p.hir_id == param_hir_id).unwrap();
1046+
1047+
// Scan the bounds and where-clauses on parameters to extract bounds
1048+
// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
1049+
// for each type parameter.
1050+
match param.kind {
1051+
GenericParamKind::Type { .. } => {
1052+
let mut set = Set1::Empty;
1053+
1054+
// Look for `type: ...` where clauses.
1055+
for bound in generics.bounds_for_param(param_def_id) {
1056+
// Ignore `for<'a> type: ...` as they can change what
1057+
// lifetimes mean (although we could "just" handle it).
1058+
if !bound.bound_generic_params.is_empty() {
1059+
continue;
1060+
}
10561061

1057-
for bound in bound.bounds {
1058-
if let hir::GenericBound::Outlives(lifetime) = bound {
1059-
set.insert(lifetime.res);
1062+
for bound in bound.bounds {
1063+
if let hir::GenericBound::Outlives(lifetime) = bound {
1064+
set.insert(lifetime.res);
1065+
}
1066+
}
10601067
}
1061-
}
1062-
}
10631068

1064-
match set {
1065-
Set1::Empty => ObjectLifetimeDefault::Empty,
1066-
Set1::One(hir::LifetimeName::Static) => ObjectLifetimeDefault::Static,
1067-
Set1::One(hir::LifetimeName::Param(param_def_id)) => {
1068-
ObjectLifetimeDefault::Param(param_def_id.to_def_id())
1069+
match set {
1070+
Set1::Empty => ObjectLifetimeDefault::Empty,
1071+
Set1::One(hir::LifetimeName::Static) => ObjectLifetimeDefault::Static,
1072+
Set1::One(hir::LifetimeName::Param(param_def_id)) => {
1073+
ObjectLifetimeDefault::Param(param_def_id.to_def_id())
1074+
}
1075+
_ => ObjectLifetimeDefault::Ambiguous,
1076+
}
1077+
}
1078+
_ => {
1079+
bug!("object_lifetime_default_raw must only be called on a type parameter")
10691080
}
1070-
_ => ObjectLifetimeDefault::Ambiguous,
10711081
}
10721082
}
1073-
_ => {
1074-
bug!("object_lifetime_default_raw must only be called on a type parameter")
1075-
}
1083+
hir::GenericParamSource::Binder => ObjectLifetimeDefault::Empty,
10761084
}
10771085
}
10781086

@@ -1392,9 +1400,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
13921400
return;
13931401
}
13941402

1395-
self.tcx
1396-
.sess
1397-
.delay_span_bug(self.tcx.hir().span(hir_id), "could not resolve {param_def_id:?}");
1403+
self.tcx.sess.delay_span_bug(
1404+
self.tcx.hir().span(hir_id),
1405+
format!("could not resolve {param_def_id:?}"),
1406+
);
13981407
}
13991408

14001409
#[instrument(level = "debug", skip(self))]

0 commit comments

Comments
 (0)