Skip to content

Commit

Permalink
Avoid expanding to unstable internal method
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Mar 29, 2024
1 parent d748046 commit 5c16969
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
17 changes: 16 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,22 @@ fn decodable_substructure(
arms.push(cx.arm(v_span, cx.pat_lit(v_span, cx.expr_usize(v_span, i)), decoded));
}

arms.push(cx.arm_unreachable(trait_span));
let expr_unreachable = cx.expr_macro_call(
trait_span,
cx.macro_call(
trait_span,
cx.path_global(
trait_span,
[sym::std, sym::unreachable]
.map(|s| Ident::new(s, trait_span))
.to_vec(),
),
ast::token::Delimiter::Parenthesis,
ast::tokenstream::TokenStream::default(),
),
);
let arm_unreachable = cx.arm(trait_span, cx.pat_wild(trait_span), expr_unreachable);
arms.push(arm_unreachable);

let result = cx.expr_ok(
trait_span,
Expand Down
37 changes: 21 additions & 16 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ impl<'a> ExtCtxt<'a> {
ast::Path { span, segments, tokens: None }
}

pub fn macro_call(
&self,
span: Span,
path: ast::Path,
delim: ast::token::Delimiter,
tokens: ast::tokenstream::TokenStream,
) -> P<ast::MacCall> {
P(ast::MacCall {
path,
args: P(ast::DelimArgs {
dspan: ast::tokenstream::DelimSpan { open: span, close: span },
delim,
tokens,
}),
})
}

pub fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
ast::MutTy { ty, mutbl }
}
Expand Down Expand Up @@ -265,6 +282,10 @@ impl<'a> ExtCtxt<'a> {
self.expr(span, ast::ExprKind::Field(expr, field))
}

pub fn expr_macro_call(&self, span: Span, call: P<ast::MacCall>) -> P<ast::Expr> {
self.expr(span, ast::ExprKind::MacCall(call))
}

pub fn expr_binary(
&self,
sp: Span,
Expand Down Expand Up @@ -410,18 +431,6 @@ impl<'a> ExtCtxt<'a> {
self.expr(sp, ast::ExprKind::Tup(exprs))
}

pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
self.expr_call_global(
span,
[sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
thin_vec![self.expr_str(span, msg)],
)
}

pub fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
}

pub fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
self.expr_call_global(sp, ok, thin_vec![expr])
Expand Down Expand Up @@ -519,10 +528,6 @@ impl<'a> ExtCtxt<'a> {
}
}

pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
}

pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: ThinVec<ast::Arm>) -> P<Expr> {
self.expr(span, ast::ExprKind::Match(arg, arms, MatchKind::Prefix))
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/derives/auxiliary/rustc-serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![crate_type = "lib"]

pub trait Decoder {
type Error;

fn read_enum<T, F>(&mut self, name: &str, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
fn read_enum_variant<T, F>(&mut self, names: &[&str], f: F)
-> Result<T, Self::Error>
where F: FnMut(&mut Self, usize) -> Result<T, Self::Error>;

}

pub trait Decodable: Sized {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>;
}
11 changes: 11 additions & 0 deletions tests/ui/derives/issue-123156.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ check-pass
//@ edition:2021
//@ aux-build:rustc-serialize.rs

#![crate_type = "lib"]
#![allow(deprecated, soft_unstable)]

extern crate rustc_serialize;

#[derive(RustcDecodable)]
pub enum Foo {}
10 changes: 10 additions & 0 deletions tests/ui/derives/issue-123156.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Future incompatibility report: Future breakage diagnostic:
warning: use of unstable library feature 'rustc_encodable_decodable': derive macro for `rustc-serialize`; should not be used in new code
--> $DIR/issue-123156.rs:10:10
|
LL | #[derive(RustcDecodable)]
| ^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #64266 <https://github.com/rust-lang/rust/issues/64266>

0 comments on commit 5c16969

Please sign in to comment.