Skip to content

Commit ff0a065

Browse files
committed
Implement dummy defines attribute that can only avoid adding anything from the signature to the list of opaques that are being defined
1 parent e552c16 commit ff0a065

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+7
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,13 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
578578
EncodeCrossCrate::No, coroutines, experimental!(coroutines)
579579
),
580580

581+
// `#[defines]` attribute to be applied to functions can and must define hidden types for
582+
// the opaque types specified in the attribute.
583+
gated!(
584+
defines, Normal, template!(List: r#"path::to::Alias, OtherAlias"#), ErrorFollowing,
585+
EncodeCrossCrate::No, type_alias_impl_trait, experimental!(defines)
586+
),
587+
581588
// `#[pointee]` attribute to designate the pointee type in SmartPointer derive-macro
582589
gated!(
583590
pointee, Normal, template!(Word), ErrorFollowing,

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ symbols! {
707707
default_method_body_is_const,
708708
default_type_parameter_fallback,
709709
default_type_params,
710+
defines,
710711
delayed_bug_from_inside_query,
711712
deny,
712713
deprecated,

compiler/rustc_ty_utils/src/opaque_types.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::bug;
77
use rustc_middle::query::Providers;
88
use rustc_middle::ty::util::{CheckRegions, NotUniqueParam};
99
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
10-
use rustc_span::Span;
10+
use rustc_span::{sym, Span};
1111
use tracing::{instrument, trace};
1212

1313
use crate::errors::{DuplicateArg, NotParam};
@@ -192,6 +192,13 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
192192
}
193193
}
194194
}
195+
196+
fn collect_taits_from_defines_attr(&mut self) -> bool {
197+
let Some(attr) = self.tcx.get_attr(self.item, sym::defines) else { return false };
198+
let attr = attr.get_normal_item();
199+
let _defines = attr.meta_kind().unwrap().defines();
200+
true
201+
}
195202
}
196203

197204
impl<'tcx> super::sig_types::SpannedTypeVisitor<'tcx> for OpaqueTypeCollector<'tcx> {
@@ -317,7 +324,9 @@ fn opaque_types_defined_by<'tcx>(
317324
let kind = tcx.def_kind(item);
318325
trace!(?kind);
319326
let mut collector = OpaqueTypeCollector::new(tcx, item);
320-
super::sig_types::walk_types(tcx, item, &mut collector);
327+
if !collector.collect_taits_from_defines_attr() {
328+
super::sig_types::walk_types(tcx, item, &mut collector);
329+
}
321330
match kind {
322331
DefKind::AssocFn
323332
| DefKind::Fn

tests/ui/type-alias-impl-trait/closure_args.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44

55
#![feature(type_alias_impl_trait)]
66

7-
mod foo {
8-
pub trait Anything {}
9-
impl<T> Anything for T {}
10-
pub type Input = impl Anything;
7+
pub trait Anything {}
8+
impl<T> Anything for T {}
9+
pub type Input = impl Anything;
1110

12-
fn bop(_: Input) {
13-
super::run(
14-
|x: u32| {
15-
println!("{x}");
16-
},
17-
0,
18-
);
19-
}
11+
fn bop(_: Input) {
12+
run(
13+
|x: u32| {
14+
println!("{x}");
15+
},
16+
0,
17+
);
2018
}
21-
use foo::Input;
2219

20+
#[defines()]
2321
fn run<F: FnOnce(Input) -> ()>(f: F, i: Input) {
2422
f(i);
2523
}

0 commit comments

Comments
 (0)