Skip to content

Commit e860813

Browse files
Return ErrorGuaranteed from items_of_instance query
Signed-off-by: FedericoBruzzone <[email protected]>
1 parent e347b48 commit e860813

File tree

9 files changed

+100
-23
lines changed

9 files changed

+100
-23
lines changed

compiler/rustc_middle/src/query/erase.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ impl EraseType for Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
164164
type Result = [u8; size_of::<Result<ty::EarlyBinder<'static, Ty<'_>>, CyclePlaceholder>>()];
165165
}
166166

167+
impl<T> EraseType for Result<(&'_ [T], &'_ [T]), rustc_errors::ErrorGuaranteed> {
168+
type Result =
169+
[u8; size_of::<Result<(&'static [()], &'static [()]), rustc_errors::ErrorGuaranteed>>()];
170+
}
171+
167172
impl<T> EraseType for Option<&'_ T> {
168173
type Result = [u8; size_of::<Option<&'static ()>>()];
169174
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,12 @@ rustc_queries! {
24922492
desc { "functions to skip for move-size check" }
24932493
}
24942494

2495-
query items_of_instance(key: (ty::Instance<'tcx>, CollectionMode)) -> (&'tcx [Spanned<MonoItem<'tcx>>], &'tcx [Spanned<MonoItem<'tcx>>]) {
2495+
query items_of_instance(
2496+
key: (ty::Instance<'tcx>, CollectionMode)
2497+
) -> Result<
2498+
(&'tcx [Spanned<MonoItem<'tcx>>], &'tcx [Spanned<MonoItem<'tcx>>]),
2499+
ErrorGuaranteed
2500+
> {
24962501
desc { "collecting items used by `{}`", key.0 }
24972502
cache_on_disk_if { true }
24982503
}

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ use rustc_middle::{bug, span_bug};
233233
use rustc_session::Limit;
234234
use rustc_session::config::EntryFnType;
235235
use rustc_span::source_map::{Spanned, dummy_spanned, respan};
236-
use rustc_span::{DUMMY_SP, Span};
236+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
237237
use tracing::{debug, instrument, trace};
238238

239239
use crate::errors::{self, EncounteredErrorWhileInstantiating, NoOptimizedMir, RecursionLimit};
@@ -466,9 +466,11 @@ fn collect_items_rec<'tcx>(
466466
));
467467

468468
rustc_data_structures::stack::ensure_sufficient_stack(|| {
469-
let (used, mentioned) = tcx.items_of_instance((instance, mode));
470-
used_items.extend(used.into_iter().copied());
471-
mentioned_items.extend(mentioned.into_iter().copied());
469+
// FIXME: we could handle the `ErrorGuaranteed` generated by `tcx.eval_instance`.
470+
if let Ok((used, mentioned)) = tcx.items_of_instance((instance, mode)) {
471+
used_items.extend(used.into_iter().copied());
472+
mentioned_items.extend(mentioned.into_iter().copied());
473+
};
472474
});
473475
}
474476
MonoItem::GlobalAsm(item_id) => {
@@ -626,6 +628,8 @@ struct MirUsedCollector<'a, 'tcx> {
626628
/// Note that this contains *not-monomorphized* items!
627629
used_mentioned_items: &'a mut UnordSet<MentionedItem<'tcx>>,
628630
instance: Instance<'tcx>,
631+
/// If an error is encountered during const evaluation.
632+
tained_by_errors: Option<ErrorGuaranteed>,
629633
}
630634

631635
impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
@@ -658,9 +662,10 @@ impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
658662
"collection encountered polymorphic constant: {:?}",
659663
const_
660664
),
661-
Err(err @ ErrorHandled::Reported(..)) => {
665+
Err(err @ ErrorHandled::Reported(reported, _)) => {
662666
err.emit_note(self.tcx);
663-
return None;
667+
self.tained_by_errors = Some(reported.into());
668+
None
664669
}
665670
}
666671
}
@@ -1211,7 +1216,7 @@ fn collect_items_of_instance<'tcx>(
12111216
tcx: TyCtxt<'tcx>,
12121217
instance: Instance<'tcx>,
12131218
mode: CollectionMode,
1214-
) -> (MonoItems<'tcx>, MonoItems<'tcx>) {
1219+
) -> Result<(MonoItems<'tcx>, MonoItems<'tcx>), ErrorGuaranteed> {
12151220
// This item is getting monomorphized, do mono-time checks.
12161221
tcx.ensure().check_mono_item(instance);
12171222

@@ -1235,6 +1240,7 @@ fn collect_items_of_instance<'tcx>(
12351240
used_items: &mut used_items,
12361241
used_mentioned_items: &mut used_mentioned_items,
12371242
instance,
1243+
tained_by_errors: None,
12381244
};
12391245

12401246
if mode == CollectionMode::UsedItems {
@@ -1260,19 +1266,23 @@ fn collect_items_of_instance<'tcx>(
12601266
}
12611267
}
12621268

1263-
(used_items, mentioned_items)
1269+
if let Some(err) = collector.tained_by_errors {
1270+
return Err(err);
1271+
}
1272+
1273+
Ok((used_items, mentioned_items))
12641274
}
12651275

12661276
fn items_of_instance<'tcx>(
12671277
tcx: TyCtxt<'tcx>,
12681278
(instance, mode): (Instance<'tcx>, CollectionMode),
1269-
) -> (&'tcx [Spanned<MonoItem<'tcx>>], &'tcx [Spanned<MonoItem<'tcx>>]) {
1270-
let (used_items, mentioned_items) = collect_items_of_instance(tcx, instance, mode);
1279+
) -> Result<(&'tcx [Spanned<MonoItem<'tcx>>], &'tcx [Spanned<MonoItem<'tcx>>]), ErrorGuaranteed> {
1280+
let (used_items, mentioned_items) = collect_items_of_instance(tcx, instance, mode)?;
12711281

12721282
let used_items = tcx.arena.alloc_from_iter(used_items);
12731283
let mentioned_items = tcx.arena.alloc_from_iter(mentioned_items);
12741284

1275-
(used_items, mentioned_items)
1285+
Ok((used_items, mentioned_items))
12761286
}
12771287

12781288
/// `item` must be already monomorphized.

src/librustdoc/html/templates/type_layout.html

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ <h2 id="layout" class="section-header"> {# #}
2828
{% endfor %}
2929
</ul>
3030
{% endif %}
31-
{# This kind of layout error can occur with valid code, e.g. if you try to
32-
get the layout of a generic type such as `Vec<T>`. #}
31+
{# This kind of layout error can occur with valid code.
32+
It can happen for various reasons, e.g., if you try to get
33+
the layout of an array with a length that is not a constant
34+
and it has not a generic parameter such as
35+
`[(); size_of::<<() as Trait>::Assoc>()]`. #}
3336
{% when Err(LayoutError::Unknown(_)) %}
3437
<p> {# #}
35-
<strong>Note:</strong> Unable to compute type layout, {#+ #}
36-
possibly due to this type having generic parameters. {#+ #}
37-
Layout can only be computed for concrete, fully-instantiated types. {# #}
38+
<strong>Note:</strong> Unable to compute type layout. {#+ #}
39+
It can happen for various reasons. {# #}
3840
</p>
3941
{# This kind of error probably can't happen with valid code, but we don't
4042
want to panic and prevent the docs from building, so we just let the
@@ -44,13 +46,13 @@ <h2 id="layout" class="section-header"> {# #}
4446
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
4547
the type was too big. {# #}
4648
</p>
47-
{# This kind of error probably can't happen with valid code, but we don't
48-
want to panic and prevent the docs from building, so we just let the
49-
user know that we couldn't compute the layout. #}
49+
{# This kind of layout error can occur with valid code, e.g. if you try to
50+
get the layout of a generic type such as `Vec<T>`. #}
5051
{% when Err(LayoutError::TooGeneric(_)) %}
5152
<p> {# #}
52-
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
53-
the type was too generic. {# #}
53+
<strong>Note:</strong> Unable to compute type layout, {#+ #}
54+
possibly due to this type having generic parameters. {#+ #}
55+
Layout can only be computed for concrete, fully-instantiated types. {# #}
5456
</p>
5557
{% when Err(LayoutError::ReferencesError(_)) %}
5658
<p> {# #}

tests/rustdoc/type-layout.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,9 @@ pub enum Uninhabited {}
9191
//@ hasraw type_layout/struct.Uninhabited2.html 'Size: '
9292
//@ hasraw - '8 bytes (<a href="https://doc.rust-lang.org/stable/reference/glossary.html#uninhabited">uninhabited</a>)'
9393
pub struct Uninhabited2(std::convert::Infallible, u64);
94+
95+
pub trait Project { type Assoc; }
96+
// We can't compute layout. A `LayoutError::Unknown` is returned.
97+
//@ hasraw type_layout/struct.Unknown.html 'Unable to compute type layout. It can happen for various reasons.'
98+
//@ !hasraw - 'Size: '
99+
pub struct Unknown(<() as Project>::Assoc) where for<'a> (): Project;

tests/ui/infinite/infinite-instantiation-struct-tail-ice-114484.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<T, const L: u8> VirtualWrapper<T, L> {
2424
impl<T: MyTrait + 'static, const L: u8> MyTrait for VirtualWrapper<T, L> {
2525
fn virtualize(&self) -> &dyn MyTrait {
2626
unsafe { virtualize_my_trait(L, self) }
27-
// unsafe { virtualize_my_trait(L, &self.0) } // <-- this code fixes the problem
27+
// unsafe { virtualize_my_trait(L, &self.0) } // <-- this code fixes the problem
2828
}
2929
}
3030

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ check-pass
2+
3+
fn pass_to_ptr_call<T>(f: fn(T), x: T) {
4+
f(x);
5+
}
6+
7+
trait TrackedTrait {
8+
fn trait_tracked_unit_default(_: ()) {
9+
let location = std::panic::Location::caller();
10+
assert_eq!(location.file(), file!());
11+
}
12+
}
13+
14+
impl TrackedTrait for () {}
15+
16+
fn main() {
17+
pass_to_ptr_call(<() as TrackedTrait>::trait_tracked_unit_default, ());
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
3+
trait Foo<T> {
4+
fn print<'a>(&'a self) where T: 'a { println!("foo"); }
5+
}
6+
7+
impl<'a> Foo<&'a ()> for () { }
8+
9+
trait Bar: for<'a> Foo<&'a ()> { }
10+
11+
impl Bar for () {}
12+
13+
fn main() {
14+
(&() as &dyn Bar).print();
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ check-pass
2+
3+
// When -Zprint-mono-items=eager option was used, the following
4+
// code previously caused an ICE.
5+
6+
pub struct S();
7+
8+
pub trait X {
9+
fn unused3(var: i32) {
10+
println!("{}", var);
11+
}
12+
}
13+
14+
impl X for S {}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)