Skip to content
Closed
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
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 @@ -394,6 +394,13 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
may_dangle, Normal, template!(Word), dropck_eyepatch,
"`may_dangle` has unstable semantics and may be removed in the future",
),
rustc_attr!(
rustc_per_edition, AssumedUsed, template!(Word),
"#[rustc_per_edition] can be applied to a type alias to make the resolution
dependent on the edition of the crate using this type alias.
It must be applied on an alias for a tuple, of which the fields correspond
to the Rust editions.",
),

// ==========================================================================
// Internal attributes: Runtime related:
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 @@ -996,6 +996,7 @@ symbols! {
rustc_peek_liveness,
rustc_peek_maybe_init,
rustc_peek_maybe_uninit,
rustc_per_edition,
rustc_polymorphize_error,
rustc_private,
rustc_proc_macro_decls,
Expand Down
36 changes: 29 additions & 7 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use rustc_middle::ty::GenericParamDefKind;
use rustc_middle::ty::{self, Const, DefIdTree, Ty, TyCtxt, TypeFoldable};
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
use rustc_span::lev_distance::find_best_match_for_name;
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{edition, Span, DUMMY_SP};
use rustc_target::spec::abi;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::astconv_object_safety_violations;
Expand Down Expand Up @@ -2053,12 +2053,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let substs = self.ast_path_substs_for_ty(span, did, item_segment.0);
self.normalize_ty(span, tcx.mk_opaque(did, substs))
}
Res::Def(DefKind::TyAlias, did) => {
assert_eq!(opt_self_ty, None);
self.prohibit_generics(path.segments.split_last().unwrap().1);
let ty = self.ast_path_to_ty(span, did, path.segments.last().unwrap());
if self.tcx().has_attr(did, sym::rustc_per_edition) {
let ty = if let ty::Tuple(..) = ty.kind() {
let mut fields = ty.tuple_fields();
let edition = span.edition();
edition::ALL_EDITIONS
.iter()
.take_while(|&&e| e != edition)
.fold(fields.next(), |ty, _| fields.next().or(ty))
} else {
None
};
ty.unwrap_or_else(|| {
self.tcx().sess.span_err(
self.tcx().def_span(did),
"#[rustc_per_edition] type alias needs to be a tuple of at least one field",
);
tcx.ty_error()
})
} else {
ty
}
}
Res::Def(
DefKind::Enum
| DefKind::TyAlias
| DefKind::Struct
| DefKind::Union
| DefKind::ForeignTy,
DefKind::Enum | DefKind::Struct | DefKind::Union | DefKind::ForeignTy,
did,
) => {
assert_eq!(opt_self_ty, None);
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/editions/auxiliary/per-edition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// edition:2015

#![feature(rustc_attrs)]

#[rustc_per_edition]
pub type I32OrStr = (
i32, // 2015
&'static str, // 2018+
);

pub type I32 = I32OrStr;

pub use I32OrStr as Magic;

#[macro_export]
macro_rules! int {
() => {
$crate::I32OrStr
}
}

#[macro_export]
macro_rules! x {
() => {
X
}
}
24 changes: 24 additions & 0 deletions src/test/ui/editions/rustc-per-edition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// check-pass
// edition:2021
// aux-build:per-edition.rs

#![feature(rustc_attrs)]

#[macro_use]
extern crate per_edition;

#[rustc_per_edition]
type X = (
u32, // 2015
&'static str, // 2018,
f64, // 2021+
);

fn main() {
let _: X = 6.28;
let _: per_edition::I32 = 1i32;
let _: per_edition::I32OrStr = "hello";
let _: per_edition::Magic = "world";
let _: per_edition::int!() = 2i32;
let _: per_edition::x!() = 3u32;
}