Skip to content

Commit 6eeff9a

Browse files
committed
Auto merge of #163227 - jhpratt:rollup-jNR52me, r=jhpratt
Rollup of 10 pull requests Successful merges: - #161988 (trait solver: Handle reflexive region constraints) - #162498 (Avoid ICEs when recovering malformed function parameters) - #163181 (revert #162715 due to #163149) - #163207 (Update deprecated rustc_hir imports 2) - #163216 (Add `inline` attribute to `NonZero::from_str`) - #120589 (std::thread::available_parallelism merging linux/android/freebsd version) - #162103 (fix(parser): emit targeted diagnostic for fields inside traits) - #162340 (When trying to recover from `for<…> dyn …` don't unconditionally treat `dyn` as a keyword) - #163094 (Add regression test for first token source file from TokenStream FromStr) - #163115 (Point at `'static` obligations from an fn being called) Failed merges: - #162942 (Remove `StashKey::AssociatedTypeSuggestion`)
2 parents 1a8fa55 + 81781bc commit 6eeff9a

157 files changed

Lines changed: 1902 additions & 351 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.

‎Cargo.lock‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3872,6 +3872,7 @@ dependencies = [
38723872
"rustc_abi",
38733873
"rustc_apfloat",
38743874
"rustc_ast",
3875+
"rustc_attr_ir",
38753876
"rustc_data_structures",
38763877
"rustc_errors",
38773878
"rustc_hir",
@@ -4129,6 +4130,7 @@ dependencies = [
41294130
"rustc_abi",
41304131
"rustc_arena",
41314132
"rustc_ast",
4133+
"rustc_attr_ir",
41324134
"rustc_data_structures",
41334135
"rustc_errors",
41344136
"rustc_feature",
@@ -4204,6 +4206,7 @@ name = "rustc_incremental"
42044206
version = "0.0.0"
42054207
dependencies = [
42064208
"rand 0.9.3",
4209+
"rustc_attr_ir",
42074210
"rustc_data_structures",
42084211
"rustc_errors",
42094212
"rustc_fs_util",
@@ -4325,6 +4328,7 @@ dependencies = [
43254328
"rustc_apfloat",
43264329
"rustc_ast",
43274330
"rustc_ast_pretty",
4331+
"rustc_attr_ir",
43284332
"rustc_attr_parsing",
43294333
"rustc_data_structures",
43304334
"rustc_errors",
@@ -4403,6 +4407,7 @@ dependencies = [
44034407
"odht",
44044408
"rustc_abi",
44054409
"rustc_ast",
4410+
"rustc_attr_ir",
44064411
"rustc_attr_parsing",
44074412
"rustc_crate_store",
44084413
"rustc_data_structures",
@@ -4493,10 +4498,10 @@ dependencies = [
44934498
"polonius-engine",
44944499
"regex",
44954500
"rustc_abi",
4501+
"rustc_attr_ir",
44964502
"rustc_data_structures",
44974503
"rustc_errors",
44984504
"rustc_graphviz",
4499-
"rustc_hir",
45004505
"rustc_index",
45014506
"rustc_macros",
45024507
"rustc_middle",
@@ -4539,6 +4544,7 @@ version = "0.0.0"
45394544
dependencies = [
45404545
"rustc_abi",
45414546
"rustc_ast",
4547+
"rustc_attr_ir",
45424548
"rustc_data_structures",
45434549
"rustc_errors",
45444550
"rustc_hir",
@@ -4656,6 +4662,7 @@ name = "rustc_privacy"
46564662
version = "0.0.0"
46574663
dependencies = [
46584664
"rustc_ast",
4665+
"rustc_attr_ir",
46594666
"rustc_data_structures",
46604667
"rustc_errors",
46614668
"rustc_hir",
@@ -4913,6 +4920,7 @@ dependencies = [
49134920
"itertools",
49144921
"rustc_abi",
49154922
"rustc_ast",
4923+
"rustc_attr_ir",
49164924
"rustc_crate_store",
49174925
"rustc_data_structures",
49184926
"rustc_errors",

‎compiler/rustc_borrowck/src/diagnostics/region_errors.rs‎

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Error reporting machinery for lifetime errors.
22
3-
use rustc_data_structures::fx::FxIndexSet;
3+
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
44
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, msg};
55
use rustc_hir as hir;
66
use rustc_hir::GenericBound::Trait;
77
use rustc_hir::QPath::Resolved;
88
use rustc_hir::WherePredicateKind::BoundPredicate;
9+
use rustc_hir::def::DefKind;
910
use rustc_hir::def::Res::Def;
1011
use rustc_hir::def_id::DefId;
1112
use rustc_hir::intravisit::Visitor;
@@ -391,6 +392,165 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
391392
outlives_suggestion.add_suggestion(self);
392393
}
393394

395+
/// Point at `'static` obligations from the item being called.
396+
///
397+
/// ```text
398+
/// error[E0521]: borrowed data escapes outside of function
399+
/// --> $DIR/static-impl-obligation.rs:163:9
400+
/// |
401+
/// LL | fn bar<'a>(x: &'a &'a u32) {
402+
/// | -- - `x` is only valid in the function body
403+
/// | |
404+
/// | lifetime `'a` defined here
405+
/// LL | let y: &dyn Foo = x;
406+
/// LL | y.hello();
407+
/// | ^^^^^^^^^
408+
/// | |
409+
/// | `x` escapes the function body here
410+
/// | argument requires that `'a` must outlive `'static`
411+
/// |
412+
/// note: `'static` lifetime requirement from `<(dyn o::Foo + 'static)>::hello` introduced here
413+
/// --> $DIR/static-impl-obligation.rs:158:20
414+
/// |
415+
/// LL | impl dyn Foo + 'static where Self: 'static {
416+
/// | ^^^^^^^ ^^^^^^^ lifetime requirement introduced here
417+
/// | |
418+
/// | lifetime requirement introduced here
419+
/// LL | fn hello(&'static self) where Self: 'static {}
420+
/// | ^^^^^^^^^^^^^ ^^^^^^^ lifetime requirement introduced here
421+
/// | |
422+
/// | lifetime requirement introduced here
423+
/// ```
424+
fn explain_impl_static_obligation(
425+
&self,
426+
diag: &mut Diag<'_>,
427+
ty: Ty<'tcx>,
428+
outlived_fr: RegionVid,
429+
) {
430+
let tcx = self.infcx.tcx;
431+
if self.regioncx.to_error_region(outlived_fr) != Some(tcx.lifetimes.re_static) {
432+
return;
433+
}
434+
let ty::FnDef(def_id, args) = ty.kind() else {
435+
return;
436+
};
437+
let typing_env = self.infcx.typing_env(self.infcx.param_env);
438+
let Ok(Some(instance)) = ty::Instance::try_resolve(
439+
tcx,
440+
typing_env,
441+
*def_id,
442+
self.infcx.deeply_resolve_ignoring_regions(args.no_bound_vars().unwrap()),
443+
) else {
444+
return;
445+
};
446+
let def_id = instance.def_id();
447+
let mut bounds =
448+
tcx.clauses_of(def_id)
449+
.instantiate(tcx, instance.args)
450+
.into_iter()
451+
.map(|(c, sp)| (c.skip_norm_wip().as_predicate(), sp))
452+
.filter(|(pred, _)| match pred.kind().skip_binder() {
453+
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
454+
ty::OutlivesClause(_, lt),
455+
))
456+
| ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
457+
ty::OutlivesClause(_, lt),
458+
)) if lt.is_static() => true,
459+
_ => false,
460+
})
461+
.map(|(_, sp)| sp)
462+
.collect::<Vec<Span>>();
463+
464+
let mut labels = FxHashMap::default();
465+
466+
let parent = tcx.parent(def_id);
467+
if let Some(rcvr) =
468+
tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip().inputs().skip_binder().get(0)
469+
{
470+
// Look at the receiver for `&'static self`, which introduces a `'static` obligation.
471+
// ```
472+
// impl Foo {
473+
// fn foo(&'static self) {}
474+
// ^^^^^^^^^^^^^
475+
// ```
476+
if let ty::Ref(region, _, _) = rcvr.kind()
477+
&& *region == tcx.lifetimes.re_static
478+
&& let Some(assoc) = tcx.opt_associated_item(def_id)
479+
&& assoc.is_method()
480+
{
481+
let def_span = tcx.def_span(def_id);
482+
// We have a `&'static self` receiver.
483+
if let Some(def_id) = def_id.as_local()
484+
&& let owner = tcx.expect_hir_owner_node(def_id)
485+
&& let Some(decl) = owner.fn_decl()
486+
&& let Some(ty) = decl.inputs.get(0)
487+
{
488+
// Point at the `&'static self` receiver.
489+
bounds.push(ty.span);
490+
} else if !bounds.iter().any(|sp| sp.overlaps(def_span)) {
491+
// The method is not defined on the local crate, point at the signature instead of
492+
// just the receiver as an approximation. We don't add it if there are already
493+
// other spans with overlap with the def `Span`, as the other will be more specific.
494+
bounds.push(def_span)
495+
}
496+
}
497+
498+
if let DefKind::Impl { .. } = tcx.def_kind(parent)
499+
&& let ty = tcx.type_of(parent).instantiate_identity().skip_norm_wip()
500+
&& let ty::Dynamic(_, region) = ty.kind()
501+
&& *region == tcx.lifetimes.re_static
502+
{
503+
// We have a call into a method of either `impl dyn Trait {}` or
504+
// `impl dyn Trait + 'static {}`.
505+
if let Some(def_id) = parent.as_local()
506+
&& let hir::OwnerNode::Item(item) = tcx.expect_hir_owner_node(def_id)
507+
&& let hir::ItemKind::Impl(impl_) = item.kind
508+
&& let hir::TyKind::TraitObject(_, tagged_ref) = impl_.self_ty.kind
509+
{
510+
if tagged_ref.is_static() {
511+
// impl dyn Trait + 'static {
512+
// ^^^^^^^ lifetime requirement introduced here
513+
bounds.push(tagged_ref.pointer().ident.span);
514+
} else if tagged_ref.is_implicit() {
515+
// impl dyn Trait {
516+
// ^^^^^^^^^ `dyn Trait` introduces an...
517+
bounds.push(impl_.self_ty.span);
518+
labels.insert(
519+
impl_.self_ty.span,
520+
"`dyn Trait` introduces an implicit `'static` lifetime requirement",
521+
);
522+
}
523+
} else {
524+
// Non-local `impl`, we don't have a way to differentiate between `+ 'static`
525+
// and bare `dyn Trait`. We point at the whole def `Span` for now.
526+
let def_span = tcx.def_span(parent);
527+
if !bounds.iter().any(|sp| sp.overlaps(def_span)) {
528+
bounds.push(def_span);
529+
}
530+
}
531+
}
532+
}
533+
534+
if !bounds.is_empty() {
535+
let mut multispan: MultiSpan = bounds.clone().into();
536+
for span in bounds {
537+
let label = labels.get(&span).unwrap_or(&"lifetime requirement introduced here");
538+
multispan.push_span_label(span, *label);
539+
}
540+
multispan.push_span_context(tcx.def_span(def_id).shrink_to_lo());
541+
if let DefKind::Impl { .. } | DefKind::Trait = tcx.def_kind(parent) {
542+
multispan.push_span_context(tcx.def_span(parent).shrink_to_lo());
543+
}
544+
diag.span_note(
545+
multispan,
546+
format!(
547+
"`'static` lifetime requirement from `{}` introduced here",
548+
tcx.def_path_str(def_id)
549+
),
550+
);
551+
}
552+
}
553+
394554
/// Report that `longer_fr: error_vid`, which doesn't hold,
395555
/// where `longer_fr` is a placeholder.
396556
fn report_erroneous_rvid_reaches_placeholder(
@@ -503,6 +663,10 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
503663
}
504664
};
505665

666+
if let ConstraintCategory::CallArgument(Some(ty)) = category {
667+
self.explain_impl_static_obligation(&mut diag, ty, outlived_fr);
668+
}
669+
506670
match variance_info {
507671
ty::VarianceDiagInfo::None => {}
508672
ty::VarianceDiagInfo::Invariant { ty, param_index } => {

‎compiler/rustc_const_eval/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ either = "1"
99
rustc_abi = { path = "../rustc_abi" }
1010
rustc_apfloat = "0.2.0"
1111
rustc_ast = { path = "../rustc_ast" }
12+
rustc_attr_ir = { path = "../rustc_attr_ir" }
1213
rustc_data_structures = { path = "../rustc_data_structures" }
1314
rustc_errors = { path = "../rustc_errors" }
1415
rustc_hir = { path = "../rustc_hir" }

‎compiler/rustc_const_eval/src/check_consts/check.rs‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use std::num::NonZero;
55
use std::ops::Deref;
66
use std::{assert_matches, mem};
77

8+
use rustc_attr_ir::lang_items::LangItem;
9+
use rustc_attr_ir::{ConstStability, StabilityLevel, find_attr};
810
use rustc_errors::{Diag, ErrorGuaranteed};
9-
use rustc_hir::attrs::lang_items::LangItem;
11+
use rustc_hir as hir;
1012
use rustc_hir::def::DefKind;
1113
use rustc_hir::def_id::DefId;
12-
use rustc_hir::{self as hir, find_attr};
1314
use rustc_index::bit_set::DenseBitSet;
1415
use rustc_infer::infer::TyCtxtInferExt;
1516
use rustc_middle::mir::visit::Visitor;
@@ -427,7 +428,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
427428
/// Check the const stability of the given item (fn or trait).
428429
fn check_callee_stability(&mut self, def_id: DefId) {
429430
match self.tcx.lookup_const_stability(def_id) {
430-
Some(hir::ConstStability { level: hir::StabilityLevel::Stable { .. }, .. }) => {
431+
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => {
431432
// All good.
432433
}
433434
None => {
@@ -443,8 +444,8 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
443444
});
444445
}
445446
}
446-
Some(hir::ConstStability {
447-
level: hir::StabilityLevel::Unstable { implied_by: implied_feature, issue, .. },
447+
Some(ConstStability {
448+
level: StabilityLevel::Unstable { implied_by: implied_feature, issue, .. },
448449
feature,
449450
..
450451
}) => {
@@ -858,8 +859,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
858859
});
859860
}
860861
}
861-
Some(hir::ConstStability {
862-
level: hir::StabilityLevel::Unstable { .. },
862+
Some(ConstStability {
863+
level: StabilityLevel::Unstable { .. },
863864
feature,
864865
..
865866
}) => {
@@ -878,10 +879,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
878879
const_stable_indirect,
879880
});
880881
}
881-
Some(hir::ConstStability {
882-
level: hir::StabilityLevel::Stable { .. },
883-
..
884-
}) => {
882+
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => {
885883
// All good. Note that a `#[rustc_const_stable]` intrinsic (meaning it
886884
// can be *directly* invoked from stable const code) does not always
887885
// have the `#[rustc_intrinsic_const_stable_indirect]` attribute (which controls

‎compiler/rustc_const_eval/src/check_consts/mod.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
//! has interior mutability or needs to be dropped, as well as the visitor that emits errors when
55
//! it finds operations that are invalid in a certain context.
66
7+
use rustc_attr_ir::find_attr;
78
use rustc_errors::DiagCtxtHandle;
9+
use rustc_hir as hir;
810
use rustc_hir::def_id::{DefId, LocalDefId};
9-
use rustc_hir::{self as hir, find_attr};
1011
use rustc_middle::mir;
1112
use rustc_middle::ty::{self, PolyFnSig, TyCtxt};
1213
use rustc_span::{Symbol, bug};

‎compiler/rustc_const_eval/src/check_consts/ops.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
33
use hir::ConstContext;
4+
use rustc_attr_ir::lang_items::LangItem;
45
use rustc_errors::codes::*;
56
use rustc_errors::{Applicability, Diag, MultiSpan, msg};
67
use rustc_hir as hir;
7-
use rustc_hir::attrs::lang_items::LangItem;
88
use rustc_hir::def_id::DefId;
99
use rustc_infer::infer::TyCtxtInferExt;
1010
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};

‎compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_hir::find_attr;
1+
use rustc_attr_ir::find_attr;
22
use rustc_middle::mir::visit::Visitor;
33
use rustc_middle::mir::{self, BasicBlock, Location};
44
use rustc_middle::ty::TyCtxt;

‎compiler/rustc_const_eval/src/check_consts/qualifs.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// FIXME(const_trait_impl): This API should be really reworked. It's dangerously general for
66
// having basically only two use-cases that act in different ways.
77

8+
use rustc_attr_ir::lang_items::LangItem;
89
use rustc_errors::ErrorGuaranteed;
9-
use rustc_hir::attrs::lang_items::LangItem;
1010
use rustc_infer::infer::TyCtxtInferExt;
1111
use rustc_middle::mir;
1212
use rustc_middle::mir::*;

‎compiler/rustc_const_eval/src/const_eval/fn_queries.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use rustc_attr_ir::find_attr;
12
use rustc_hir::def_id::{DefId, LocalDefId};
23
use rustc_hir::{
34
Constness, ExprKind, ForeignItemKind, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind,
4-
Node, TraitItem, TraitItemKind, VariantData, find_attr,
5+
Node, TraitItem, TraitItemKind, VariantData,
56
};
67
use rustc_middle::query::Providers;
78
use rustc_middle::ty::TyCtxt;

0 commit comments

Comments
 (0)