Rollup merge of #138911 - compiler-errors:define-opaque, r=oli-obk

Allow defining opaques in statics and consts

r? oli-obk

Fixes https://github.com/rust-lang/rust/issues/138902
This commit is contained in:
Jacob Pratt 2025-03-25 20:34:49 -04:00 committed by GitHub
commit 5bd69d940e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 362 additions and 165 deletions

View file

@ -11,15 +11,20 @@ pub(crate) fn expand(
let define_opaque = match &mut item {
Annotatable::Item(p) => match &mut p.kind {
ast::ItemKind::Fn(f) => Some(&mut f.define_opaque),
ast::ItemKind::Const(ct) => Some(&mut ct.define_opaque),
ast::ItemKind::Static(si) => Some(&mut si.define_opaque),
_ => None,
},
Annotatable::AssocItem(i, _assoc_ctxt) => match &mut i.kind {
ast::AssocItemKind::Fn(func) => Some(&mut func.define_opaque),
ast::AssocItemKind::Const(ct) => Some(&mut ct.define_opaque),
_ => None,
},
Annotatable::Stmt(s) => match &mut s.kind {
ast::StmtKind::Item(p) => match &mut p.kind {
ast::ItemKind::Fn(f) => Some(&mut f.define_opaque),
ast::ItemKind::Const(ct) => Some(&mut ct.define_opaque),
ast::ItemKind::Static(si) => Some(&mut si.define_opaque),
_ => None,
},
_ => None,
@ -47,7 +52,10 @@ pub(crate) fn expand(
.collect(),
);
} else {
ecx.dcx().span_err(meta_item.span, "only functions and methods can define opaque types");
ecx.dcx().span_err(
meta_item.span,
"only functions, statics, and consts can define opaque types",
);
}
vec![item]

View file

@ -285,6 +285,7 @@ pub(crate) fn expand_test_or_bench(
defaultness: ast::Defaultness::Final,
generics: ast::Generics::default(),
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
define_opaque: None,
// test::TestDescAndFn {
expr: Some(
cx.expr_struct(