Skip to content

hir_analysis: prohibit dyn PointeeSized #143104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ hir_analysis_parenthesized_fn_trait_expansion =
hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind}
.label = not allowed in type signatures
hir_analysis_pointee_sized_trait_object = `PointeeSized` cannot be used with trait objects
hir_analysis_precise_capture_self_alias = `Self` can't be captured in `use<...>` precise captures list, since it is an alias
.label = `Self` is not a generic argument, but an alias to the type of the {$what}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ pub(crate) struct TraitObjectDeclaredWithNoTraits {
pub trait_alias_span: Option<Span>,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_pointee_sized_trait_object)]
pub(crate) struct PointeeSizedTraitObject {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_ambiguous_lifetime_bound, code = E0227)]
pub(crate) struct AmbiguousLifetimeBound {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::codes::*;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
use rustc_hir::LangItem;
use rustc_hir::def::{DefKind, Res};
use rustc_lint_defs::builtin::UNUSED_ASSOCIATED_TYPE_BOUNDS;
use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan;
Expand Down Expand Up @@ -69,7 +70,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.into_iter()
.partition(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()));

// We don't support empty trait objects.
// We don't support empty trait objects.
if regular_traits.is_empty() && auto_traits.is_empty() {
let guar =
self.report_trait_object_with_no_traits(span, user_written_bounds.iter().copied());
Expand All @@ -80,6 +81,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let guar = self.report_trait_object_addition_traits(&regular_traits);
return Ty::new_error(tcx, guar);
}
// We don't support `PointeeSized` principals
let pointee_sized_did = tcx.require_lang_item(LangItem::PointeeSized, span);
if regular_traits.iter().any(|(pred, _)| pred.def_id() == pointee_sized_did) {
let guar = self.report_pointee_sized_trait_object(span);
return Ty::new_error(tcx, guar);
}

// Don't create a dyn trait if we have errors in the principal.
if let Err(guar) = regular_traits.error_reported() {
return Ty::new_error(tcx, guar);
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tracing::debug;
use super::InherentAssocCandidate;
use crate::errors::{
self, AssocItemConstraintsNotAllowedHere, ManualImplementation, MissingTypeParams,
ParenthesizedFnTraitExpansion, TraitObjectDeclaredWithNoTraits,
ParenthesizedFnTraitExpansion, PointeeSizedTraitObject, TraitObjectDeclaredWithNoTraits,
};
use crate::fluent_generated as fluent;
use crate::hir_ty_lowering::{AssocItemQSelf, HirTyLowerer};
Expand Down Expand Up @@ -1410,6 +1410,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {

self.dcx().emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span })
}

pub(super) fn report_pointee_sized_trait_object(&self, span: Span) -> ErrorGuaranteed {
self.dcx().emit_err(PointeeSizedTraitObject { span })
}
}

/// Emit an error for the given associated item constraint.
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/sized-hierarchy/reject-dyn-pointeesized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(sized_hierarchy)]

use std::marker::PointeeSized;

type Foo = dyn PointeeSized;
//~^ ERROR `PointeeSized` cannot be used with trait objects

fn foo(f: &Foo) {}

fn main() {
foo(&());

let x = main;
let y: Box<dyn PointeeSized> = x;
//~^ ERROR `PointeeSized` cannot be used with trait objects
}
14 changes: 14 additions & 0 deletions tests/ui/sized-hierarchy/reject-dyn-pointeesized.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: `PointeeSized` cannot be used with trait objects
--> $DIR/reject-dyn-pointeesized.rs:5:12
|
LL | type Foo = dyn PointeeSized;
| ^^^^^^^^^^^^^^^^

error: `PointeeSized` cannot be used with trait objects
--> $DIR/reject-dyn-pointeesized.rs:14:16
|
LL | let y: Box<dyn PointeeSized> = x;
| ^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Loading