Skip to content

Commit ca1f813

Browse files
committed
Auto merge of #114181 - matthiaskrgr:rollup-14m8s7f, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #114099 (privacy: no nominal visibility for assoc fns ) - #114128 (When flushing delayed span bugs, write to the ICE dump file even if it doesn't exist) - #114138 (Adjust spans correctly for fn -> method suggestion) - #114146 (Skip reporting item name when checking RPITIT GAT's associated type bounds hold) - #114147 (Insert RPITITs that were shadowed by missing ADTs that resolve to [type error]) - #114155 (Replace a lazy `RefCell<Option<T>>` with `OnceCell<T>`) - #114164 (Add regression test for `--cap-lints allow` and trait bounds warning) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 04abc37 + a4b9405 commit ca1f813

23 files changed

+413
-23
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,11 +1655,11 @@ impl HandlerInner {
16551655
let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(true, |x| &x != "0");
16561656
for bug in bugs {
16571657
if let Some(file) = self.ice_file.as_ref()
1658-
&& let Ok(mut out) = std::fs::File::options().append(true).open(file)
1658+
&& let Ok(mut out) = std::fs::File::options().create(true).append(true).open(file)
16591659
{
16601660
let _ = write!(
16611661
&mut out,
1662-
"\n\ndelayed span bug: {}\n{}",
1662+
"delayed span bug: {}\n{}\n",
16631663
bug.inner.styled_message().iter().filter_map(|(msg, _)| msg.as_str()).collect::<String>(),
16641664
&bug.note
16651665
);

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
754754
);
755755
ocx.resolve_regions_and_report_errors(impl_m_def_id, &outlives_env)?;
756756

757-
let mut collected_tys = FxHashMap::default();
757+
let mut remapped_types = FxHashMap::default();
758758
for (def_id, (ty, args)) in collected_types {
759759
match infcx.fully_resolve((ty, args)) {
760760
Ok((ty, args)) => {
@@ -804,19 +804,37 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
804804
Ok(ty) => ty,
805805
Err(guar) => Ty::new_error(tcx, guar),
806806
};
807-
collected_tys.insert(def_id, ty::EarlyBinder::bind(ty));
807+
remapped_types.insert(def_id, ty::EarlyBinder::bind(ty));
808808
}
809809
Err(err) => {
810810
let reported = tcx.sess.delay_span_bug(
811811
return_span,
812812
format!("could not fully resolve: {ty} => {err:?}"),
813813
);
814-
collected_tys.insert(def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, reported)));
814+
remapped_types.insert(def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, reported)));
815815
}
816816
}
817817
}
818818

819-
Ok(&*tcx.arena.alloc(collected_tys))
819+
// We may not collect all RPITITs that we see in the HIR for a trait signature
820+
// because an RPITIT was located within a missing item. Like if we have a sig
821+
// returning `-> Missing<impl Sized>`, that gets converted to `-> [type error]`,
822+
// and when walking through the signature we end up never collecting the def id
823+
// of the `impl Sized`. Insert that here, so we don't ICE later.
824+
for assoc_item in tcx.associated_types_for_impl_traits_in_associated_fn(trait_m.def_id) {
825+
if !remapped_types.contains_key(assoc_item) {
826+
remapped_types.insert(
827+
*assoc_item,
828+
ty::EarlyBinder::bind(Ty::new_error_with_message(
829+
tcx,
830+
return_span,
831+
"missing synthetic item for RPITIT",
832+
)),
833+
);
834+
}
835+
}
836+
837+
Ok(&*tcx.arena.alloc(remapped_types))
820838
}
821839

822840
struct ImplTraitInTraitCollector<'a, 'tcx> {

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
531531
return;
532532
}
533533

534-
let up_to_rcvr_span = segment.ident.span.until(callee_expr.span);
535-
let rest_span = callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
534+
let Some(callee_expr_span) = callee_expr.span.find_ancestor_inside(call_expr.span)
535+
else {
536+
return;
537+
};
538+
let up_to_rcvr_span = segment.ident.span.until(callee_expr_span);
539+
let rest_span = callee_expr_span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
536540
let rest_snippet = if let Some(first) = rest.first() {
537541
self.tcx
538542
.sess

compiler/rustc_infer/src/infer/error_reporting/note.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
243243
}
244244
infer::CheckAssociatedTypeBounds { impl_item_def_id, trait_item_def_id, parent } => {
245245
let mut err = self.report_concrete_failure(*parent, sub, sup);
246-
let trait_item_span = self.tcx.def_span(trait_item_def_id);
247-
let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
248-
err.span_label(
249-
trait_item_span,
250-
format!("definition of `{}` from trait", item_name),
251-
);
246+
247+
// Don't mention the item name if it's an RPITIT, since that'll just confuse
248+
// folks.
249+
if !self.tcx.is_impl_trait_in_trait(impl_item_def_id.to_def_id()) {
250+
let trait_item_span = self.tcx.def_span(trait_item_def_id);
251+
let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
252+
err.span_label(
253+
trait_item_span,
254+
format!("definition of `{}` from trait", item_name),
255+
);
256+
}
257+
252258
self.suggest_copy_trait_method_bounds(
253259
trait_item_def_id,
254260
impl_item_def_id,

compiler/rustc_middle/src/middle/privacy.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,20 @@ impl EffectiveVisibilities {
178178
// All effective visibilities except `reachable_through_impl_trait` are limited to
179179
// nominal visibility. For some items nominal visibility doesn't make sense so we
180180
// don't check this condition for them.
181-
if !matches!(tcx.def_kind(def_id), DefKind::Impl { .. }) {
181+
let is_impl = matches!(tcx.def_kind(def_id), DefKind::Impl { .. });
182+
let is_associated_item_in_trait_impl = tcx
183+
.impl_of_method(def_id.to_def_id())
184+
.and_then(|impl_id| tcx.trait_id_of_impl(impl_id))
185+
.is_some();
186+
if !is_impl && !is_associated_item_in_trait_impl {
182187
let nominal_vis = tcx.visibility(def_id);
183188
if !nominal_vis.is_at_least(ev.reachable, tcx) {
184189
span_bug!(
185190
span,
186191
"{:?}: reachable {:?} > nominal {:?}",
187192
def_id,
188193
ev.reachable,
189-
nominal_vis
194+
nominal_vis,
190195
);
191196
}
192197
}

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::TyCtxt;
1111
use rustc_span::source_map::original_sp;
1212
use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol};
1313

14-
use std::cell::RefCell;
14+
use std::cell::OnceCell;
1515
use std::cmp::Ordering;
1616

1717
#[derive(Debug, Copy, Clone)]
@@ -67,7 +67,7 @@ impl CoverageStatement {
6767
pub(super) struct CoverageSpan {
6868
pub span: Span,
6969
pub expn_span: Span,
70-
pub current_macro_or_none: RefCell<Option<Option<Symbol>>>,
70+
pub current_macro_or_none: OnceCell<Option<Symbol>>,
7171
pub bcb: BasicCoverageBlock,
7272
pub coverage_statements: Vec<CoverageStatement>,
7373
pub is_closure: bool,
@@ -175,8 +175,7 @@ impl CoverageSpan {
175175
/// If the span is part of a macro, returns the macro name symbol.
176176
pub fn current_macro(&self) -> Option<Symbol> {
177177
self.current_macro_or_none
178-
.borrow_mut()
179-
.get_or_insert_with(|| {
178+
.get_or_init(|| {
180179
if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
181180
self.expn_span.ctxt().outer_expn_data().kind
182181
{

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,16 @@ fn associated_type_for_impl_trait_in_impl(
346346
) -> LocalDefId {
347347
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
348348

349-
// FIXME fix the span, we probably want the def_id of the return type of the function
350-
let span = tcx.def_span(impl_fn_def_id);
349+
let decl = tcx
350+
.hir()
351+
.find_by_def_id(impl_fn_def_id)
352+
.expect("expected item")
353+
.fn_decl()
354+
.expect("expected decl");
355+
let span = match decl.output {
356+
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
357+
hir::FnRetTy::Return(ty) => ty.span,
358+
};
351359
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy);
352360

353361
let local_def_id = impl_assoc_ty.def_id();

library/std/src/panicking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
300300
};
301301

302302
if let Some(path) = path
303-
&& let Ok(mut out) = crate::fs::File::options().create(true).write(true).open(&path)
303+
&& let Ok(mut out) = crate::fs::File::options().create(true).append(true).open(&path)
304304
{
305305
write(&mut out, BacktraceStyle::full());
306306
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// issue: 114146
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
5+
trait Foo {
6+
fn bar<'other: 'a>() -> impl Sized + 'a {}
7+
//~^ ERROR use of undeclared lifetime name `'a`
8+
//~| ERROR use of undeclared lifetime name `'a`
9+
}
10+
11+
fn main() {}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0261]: use of undeclared lifetime name `'a`
2+
--> $DIR/bad-item-bound-within-rpitit-2.rs:6:20
3+
|
4+
LL | fn bar<'other: 'a>() -> impl Sized + 'a {}
5+
| ^^ undeclared lifetime
6+
|
7+
help: consider introducing lifetime `'a` here
8+
|
9+
LL | fn bar<'a, 'other: 'a>() -> impl Sized + 'a {}
10+
| +++
11+
help: consider introducing lifetime `'a` here
12+
|
13+
LL | trait Foo<'a> {
14+
| ++++
15+
16+
error[E0261]: use of undeclared lifetime name `'a`
17+
--> $DIR/bad-item-bound-within-rpitit-2.rs:6:42
18+
|
19+
LL | fn bar<'other: 'a>() -> impl Sized + 'a {}
20+
| ^^ undeclared lifetime
21+
|
22+
help: consider introducing lifetime `'a` here
23+
|
24+
LL | fn bar<'a, 'other: 'a>() -> impl Sized + 'a {}
25+
| +++
26+
help: consider introducing lifetime `'a` here
27+
|
28+
LL | trait Foo<'a> {
29+
| ++++
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)