Skip to content

Commit

Permalink
Implement dummy defines attribute that can only avoid adding anythi…
Browse files Browse the repository at this point in the history
…ng from the signature to the list of opaques that are being defined
  • Loading branch information
oli-obk committed Dec 4, 2024
1 parent a91c361 commit 2e3f62c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
7 changes: 7 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,13 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
EncodeCrossCrate::No, coroutines, experimental!(coroutines)
),

// `#[defines]` attribute to be applied to functions can and must define hidden types for
// the opaque types specified in the attribute.
gated!(
defines, Normal, template!(List: r#"path::to::Alias, OtherAlias"#), ErrorFollowing,
EncodeCrossCrate::No, type_alias_impl_trait, experimental!(defines)
),

// RFC 3543
// `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
gated!(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ symbols! {
default_method_body_is_const,
default_type_parameter_fallback,
default_type_params,
defines,
delayed_bug_from_inside_query,
deny,
deprecated,
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_ty_utils/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::bug;
use rustc_middle::query::Providers;
use rustc_middle::ty::util::{CheckRegions, NotUniqueParam};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
use rustc_span::Span;
use rustc_span::{Span, sym};
use tracing::{instrument, trace};

use crate::errors::{DuplicateArg, NotParam};
Expand Down Expand Up @@ -192,6 +192,11 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
}
}
}

fn collect_taits_from_defines_attr(&mut self) -> bool {
let Some(_attr) = self.tcx.get_attr(self.item, sym::defines) else { return false };
true
}
}

impl<'tcx> super::sig_types::SpannedTypeVisitor<'tcx> for OpaqueTypeCollector<'tcx> {
Expand Down Expand Up @@ -317,7 +322,9 @@ fn opaque_types_defined_by<'tcx>(
let kind = tcx.def_kind(item);
trace!(?kind);
let mut collector = OpaqueTypeCollector::new(tcx, item);
super::sig_types::walk_types(tcx, item, &mut collector);
if !collector.collect_taits_from_defines_attr() {
super::sig_types::walk_types(tcx, item, &mut collector);
}
match kind {
DefKind::AssocFn
| DefKind::Fn
Expand Down
24 changes: 11 additions & 13 deletions tests/ui/type-alias-impl-trait/closure_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

#![feature(type_alias_impl_trait)]

mod foo {
pub trait Anything {}
impl<T> Anything for T {}
pub type Input = impl Anything;
pub trait Anything {}
impl<T> Anything for T {}
pub type Input = impl Anything;

fn bop(_: Input) {
super::run(
|x: u32| {
println!("{x}");
},
0,
);
}
fn bop(_: Input) {
run(
|x: u32| {
println!("{x}");
},
0,
);
}
use foo::Input;

#[defines()]
fn run<F: FnOnce(Input) -> ()>(f: F, i: Input) {
f(i);
}
Expand Down

0 comments on commit 2e3f62c

Please sign in to comment.