Skip to content

Commit 69b2e27

Browse files
authored
Unrolled build for #143104
Rollup merge of #143104 - davidtwco:issue-142652-dyn-pointeesized-deny, r=compiler-errors hir_analysis: prohibit `dyn PointeeSized` Fixes #142652 Supersedes #142663 `dyn PointeeSized` is nonsensical as a `dyn PointeeSized` needs to be `MetaSized`, so lets reject it to avoid hitting code paths that expect a builtin impl for `PointeeSized` r? `@compiler-errors`
2 parents bdaba05 + 2057423 commit 69b2e27

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ hir_analysis_parenthesized_fn_trait_expansion =
447447
448448
hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind}
449449
.label = not allowed in type signatures
450+
451+
hir_analysis_pointee_sized_trait_object = `PointeeSized` cannot be used with trait objects
452+
450453
hir_analysis_precise_capture_self_alias = `Self` can't be captured in `use<...>` precise captures list, since it is an alias
451454
.label = `Self` is not a generic argument, but an alias to the type of the {$what}
452455

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ pub(crate) struct TraitObjectDeclaredWithNoTraits {
318318
pub trait_alias_span: Option<Span>,
319319
}
320320

321+
#[derive(Diagnostic)]
322+
#[diag(hir_analysis_pointee_sized_trait_object)]
323+
pub(crate) struct PointeeSizedTraitObject {
324+
#[primary_span]
325+
pub span: Span,
326+
}
327+
321328
#[derive(Diagnostic)]
322329
#[diag(hir_analysis_ambiguous_lifetime_bound, code = E0227)]
323330
pub(crate) struct AmbiguousLifetimeBound {

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
22
use rustc_errors::codes::*;
33
use rustc_errors::struct_span_code_err;
44
use rustc_hir as hir;
5+
use rustc_hir::LangItem;
56
use rustc_hir::def::{DefKind, Res};
67
use rustc_lint_defs::builtin::UNUSED_ASSOCIATED_TYPE_BOUNDS;
78
use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan;
@@ -69,7 +70,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
6970
.into_iter()
7071
.partition(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()));
7172

72-
// We don't support empty trait objects.
73+
// We don't support empty trait objects.
7374
if regular_traits.is_empty() && auto_traits.is_empty() {
7475
let guar =
7576
self.report_trait_object_with_no_traits(span, user_written_bounds.iter().copied());
@@ -80,6 +81,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
8081
let guar = self.report_trait_object_addition_traits(&regular_traits);
8182
return Ty::new_error(tcx, guar);
8283
}
84+
// We don't support `PointeeSized` principals
85+
let pointee_sized_did = tcx.require_lang_item(LangItem::PointeeSized, span);
86+
if regular_traits.iter().any(|(pred, _)| pred.def_id() == pointee_sized_did) {
87+
let guar = self.report_pointee_sized_trait_object(span);
88+
return Ty::new_error(tcx, guar);
89+
}
90+
8391
// Don't create a dyn trait if we have errors in the principal.
8492
if let Err(guar) = regular_traits.error_reported() {
8593
return Ty::new_error(tcx, guar);

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use tracing::debug;
2929
use super::InherentAssocCandidate;
3030
use crate::errors::{
3131
self, AssocItemConstraintsNotAllowedHere, ManualImplementation, MissingTypeParams,
32-
ParenthesizedFnTraitExpansion, TraitObjectDeclaredWithNoTraits,
32+
ParenthesizedFnTraitExpansion, PointeeSizedTraitObject, TraitObjectDeclaredWithNoTraits,
3333
};
3434
use crate::fluent_generated as fluent;
3535
use crate::hir_ty_lowering::{AssocItemQSelf, HirTyLowerer};
@@ -1410,6 +1410,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14101410

14111411
self.dcx().emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span })
14121412
}
1413+
1414+
pub(super) fn report_pointee_sized_trait_object(&self, span: Span) -> ErrorGuaranteed {
1415+
self.dcx().emit_err(PointeeSizedTraitObject { span })
1416+
}
14131417
}
14141418

14151419
/// Emit an error for the given associated item constraint.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(sized_hierarchy)]
2+
3+
use std::marker::PointeeSized;
4+
5+
type Foo = dyn PointeeSized;
6+
//~^ ERROR `PointeeSized` cannot be used with trait objects
7+
8+
fn foo(f: &Foo) {}
9+
10+
fn main() {
11+
foo(&());
12+
13+
let x = main;
14+
let y: Box<dyn PointeeSized> = x;
15+
//~^ ERROR `PointeeSized` cannot be used with trait objects
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: `PointeeSized` cannot be used with trait objects
2+
--> $DIR/reject-dyn-pointeesized.rs:5:12
3+
|
4+
LL | type Foo = dyn PointeeSized;
5+
| ^^^^^^^^^^^^^^^^
6+
7+
error: `PointeeSized` cannot be used with trait objects
8+
--> $DIR/reject-dyn-pointeesized.rs:14:16
9+
|
10+
LL | let y: Box<dyn PointeeSized> = x;
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)