Skip to content

Commit 17d35ca

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 37e7459 commit 17d35ca

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+7
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,13 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
573573
EncodeCrossCrate::No, coroutines, experimental!(coroutines)
574574
),
575575

576+
// `#[defines]` attribute to be applied to functions can and must define hidden types for
577+
// the opaque types specified in the attribute.
578+
gated!(
579+
defines, Normal, template!(List: r#"path::to::Alias, OtherAlias"#), ErrorFollowing,
580+
EncodeCrossCrate::No, type_alias_impl_trait, experimental!(defines)
581+
),
582+
576583
// RFC 3543
577584
// `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
578585
gated!(

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ symbols! {
736736
default_method_body_is_const,
737737
default_type_parameter_fallback,
738738
default_type_params,
739+
defines,
739740
delayed_bug_from_inside_query,
740741
deny,
741742
deprecated,

compiler/rustc_ty_utils/src/opaque_types.rs

+9-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::{Span, sym};
1111
use tracing::{instrument, trace};
1212

1313
use crate::errors::{DuplicateArg, NotParam};
@@ -192,6 +192,11 @@ 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+
true
199+
}
195200
}
196201

197202
impl<'tcx> super::sig_types::SpannedTypeVisitor<'tcx> for OpaqueTypeCollector<'tcx> {
@@ -317,7 +322,9 @@ fn opaque_types_defined_by<'tcx>(
317322
let kind = tcx.def_kind(item);
318323
trace!(?kind);
319324
let mut collector = OpaqueTypeCollector::new(tcx, item);
320-
super::sig_types::walk_types(tcx, item, &mut collector);
325+
if !collector.collect_taits_from_defines_attr() {
326+
super::sig_types::walk_types(tcx, item, &mut collector);
327+
}
321328
match kind {
322329
DefKind::AssocFn
323330
| 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)