Skip to content

Commit ca1f813

Browse files
committed
Auto merge of rust-lang#114181 - matthiaskrgr:rollup-14m8s7f, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#114099 (privacy: no nominal visibility for assoc fns ) - rust-lang#114128 (When flushing delayed span bugs, write to the ICE dump file even if it doesn't exist) - rust-lang#114138 (Adjust spans correctly for fn -> method suggestion) - rust-lang#114146 (Skip reporting item name when checking RPITIT GAT's associated type bounds hold) - rust-lang#114147 (Insert RPITITs that were shadowed by missing ADTs that resolve to [type error]) - rust-lang#114155 (Replace a lazy `RefCell<Option<T>>` with `OnceCell<T>`) - rust-lang#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

+2-2
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

+22-4
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

+6-2
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

+12-6
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

+7-2
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

+3-4
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

+10-2
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

+1-1
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
}
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() {}
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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// issue: 114145
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
5+
trait Iterable {
6+
type Item<'a>
7+
where
8+
Self: 'a;
9+
10+
fn iter(&self) -> impl '_ + Iterator<Item = Self::Item<'_>>;
11+
}
12+
13+
impl<'a, I: 'a + Iterable> Iterable for &'a I {
14+
type Item<'b> = I::Item<'a>
15+
where
16+
'b: 'a;
17+
//~^ ERROR impl has stricter requirements than trait
18+
19+
fn iter(&self) -> impl 'a + Iterator<Item = I::Item<'a>> {
20+
//~^ ERROR the type `&'a I` does not fulfill the required lifetime
21+
(*self).iter()
22+
}
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0276]: impl has stricter requirements than trait
2+
--> $DIR/bad-item-bound-within-rpitit.rs:16:13
3+
|
4+
LL | type Item<'a>
5+
| ------------- definition of `Item` from trait
6+
...
7+
LL | 'b: 'a;
8+
| ^^ impl has extra requirement `'b: 'a`
9+
|
10+
help: copy the `where` clause predicates from the trait
11+
|
12+
LL | where Self: 'b;
13+
| ~~~~~~~~~~~~~~
14+
15+
error[E0477]: the type `&'a I` does not fulfill the required lifetime
16+
--> $DIR/bad-item-bound-within-rpitit.rs:19:23
17+
|
18+
LL | fn iter(&self) -> impl 'a + Iterator<Item = I::Item<'a>> {
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
note: type must outlive the anonymous lifetime as defined here
22+
--> $DIR/bad-item-bound-within-rpitit.rs:10:28
23+
|
24+
LL | fn iter(&self) -> impl '_ + Iterator<Item = Self::Item<'_>>;
25+
| ^^
26+
27+
error: aborting due to 2 previous errors
28+
29+
Some errors have detailed explanations: E0276, E0477.
30+
For more information about an error, try `rustc --explain E0276`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// issue: 113903
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
5+
use std::ops::Deref;
6+
7+
pub trait Tr {
8+
fn w() -> impl Deref<Target = Missing<impl Sized>>;
9+
//~^ ERROR cannot find type `Missing` in this scope
10+
}
11+
12+
impl Tr for () {
13+
fn w() -> &'static () {
14+
&()
15+
}
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0412]: cannot find type `Missing` in this scope
2+
--> $DIR/rpitit-shadowed-by-missing-adt.rs:8:35
3+
|
4+
LL | fn w() -> impl Deref<Target = Missing<impl Sized>>;
5+
| ^^^^^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0412`.
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/43134
2+
3+
// check-pass
4+
// compile-flags: --cap-lints allow
5+
6+
type Foo<T: Clone> = Option<T>;
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// issue: 114131
2+
3+
fn main() {
4+
let hello = len(vec![]);
5+
//~^ ERROR cannot find function `len` in this scope
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0425]: cannot find function `len` in this scope
2+
--> $DIR/suggest-method-on-call-with-macro-rcvr.rs:4:17
3+
|
4+
LL | let hello = len(vec![]);
5+
| ^^^ not found in this scope
6+
|
7+
help: use the `.` operator to call the method `len` on `&Vec<_>`
8+
|
9+
LL - let hello = len(vec![]);
10+
LL + let hello = vec![].len();
11+
|
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0425`.

tests/ui/privacy/issue-113860-1.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(staged_api)]
2+
//~^ ERROR module has missing stability attribute
3+
4+
pub trait Trait {
5+
//~^ ERROR trait has missing stability attribute
6+
fn fun() {}
7+
//~^ ERROR associated function has missing stability attribute
8+
}
9+
10+
impl Trait for u8 {
11+
//~^ ERROR implementation has missing stability attribute
12+
pub(self) fn fun() {}
13+
//~^ ERROR visibility qualifiers are not permitted here [E0449]
14+
}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)