Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 8 pull requests #89901

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
11140ff
Stabilize `unreachable_unchecked` as `const fn`
jhpratt Oct 4, 2021
df03b08
move implicit `Sized` predicate to end of list
tlyu Jun 4, 2021
c07f5c4
update ui test expectations
tlyu Oct 8, 2021
1afe14c
add `slice::swap_unchecked`
ibraheemdev Aug 31, 2021
14769ce
enable `slice_swap_unchecked` feature in doc test
ibraheemdev Aug 31, 2021
33ecc33
use `swap_unchecked` in `slice::reverse`
ibraheemdev Sep 1, 2021
2a8ff8d
improve slice::swap panic message
ibraheemdev Oct 11, 2021
c517a0d
add slice::swap tests
ibraheemdev Oct 11, 2021
32d4762
Remove redundant matching
camsteffen Oct 13, 2021
af5b146
Use non-checking TLS relocation in aarch64 asm! sym test.
adamgemmell Oct 13, 2021
cf12732
don't duplicate slice `panic_bounds_check`
ibraheemdev Oct 14, 2021
c5a68cf
add dedicated error variant for writing the discriminant of an uninha…
RalfJung Oct 13, 2021
cca3914
add long explanation for E0183
cameron1024 Oct 14, 2021
601197f
Remove trailing semicolon from macro call span
camsteffen Oct 13, 2021
d8f9b25
Guess semicolon span for macro statements
camsteffen Oct 14, 2021
a2a5c17
Bless tests
camsteffen Oct 14, 2021
6dcd78b
Fix clippy with changed macro statement spans
camsteffen Oct 14, 2021
64d18d4
Remove unused dependencies from rustc_const_eval
camsteffen Oct 7, 2021
7f94289
Rollup merge of #86011 - tlyu:correct-sized-bound-spans, r=estebank
JohnTitor Oct 15, 2021
557d8b1
Rollup merge of #88540 - ibraheemdev:swap-unchecked, r=kennytm
JohnTitor Oct 15, 2021
d9a9d6e
Rollup merge of #89509 - jhpratt:stabilize-const_unreachable_unchecke…
JohnTitor Oct 15, 2021
0082c26
Rollup merge of #89859 - RalfJung:write-discriminant, r=oli-obk
JohnTitor Oct 15, 2021
c3e6983
Rollup merge of #89860 - camsteffen:macro-semi, r=petrochenkov
JohnTitor Oct 15, 2021
08a832a
Rollup merge of #89880 - adamgemmell:dev/nc-relocation, r=Amanieu
JohnTitor Oct 15, 2021
daa78ee
Rollup merge of #89885 - cameron1024:long-explanation-E0183, r=Guilla…
JohnTitor Oct 15, 2021
4e19d4c
Rollup merge of #89894 - camsteffen:unused-deps, r=Mark-Simulacrum
JohnTitor Oct 15, 2021
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
2 changes: 0 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3736,8 +3736,6 @@ dependencies = [
name = "rustc_const_eval"
version = "0.0.0"
dependencies = [
"either",
"gsgdt",
"rustc_apfloat",
"rustc_ast",
"rustc_attr",
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_const_eval/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ edition = "2021"
doctest = false

[dependencies]
either = "1.5.0"
gsgdt = "0.1.2"
tracing = "0.1"
rustc_apfloat = { path = "../rustc_apfloat" }
rustc_ast = { path = "../rustc_ast" }
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}

/// Read discriminant, return the runtime value as well as the variant index.
/// Can also legally be called on non-enums (e.g. through the discriminant_value intrinsic)!
pub fn read_discriminant(
&self,
op: &OpTy<'tcx, M::PointerTag>,
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_const_eval/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,10 +988,23 @@ where
variant_index: VariantIdx,
dest: &PlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> {
// This must be an enum or generator.
match dest.layout.ty.kind() {
ty::Adt(adt, _) => assert!(adt.is_enum()),
ty::Generator(..) => {}
_ => span_bug!(
self.cur_span(),
"write_discriminant called on non-variant-type (neither enum nor generator)"
),
}
// Layout computation excludes uninhabited variants from consideration
// therefore there's no way to represent those variants in the given layout.
// Essentially, uninhabited variants do not have a tag that corresponds to their
// discriminant, so we cannot do anything here.
// When evaluating we will always error before even getting here, but ConstProp 'executes'
// dead code, so we cannot ICE here.
if dest.layout.for_variant(self, variant_index).abi.is_uninhabited() {
throw_ub!(Unreachable);
throw_ub!(UninhabitedEnumVariantWritten)
}

match dest.layout.variants {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ E0164: include_str!("./error_codes/E0164.md"),
E0165: include_str!("./error_codes/E0165.md"),
E0170: include_str!("./error_codes/E0170.md"),
E0178: include_str!("./error_codes/E0178.md"),
E0183: include_str!("./error_codes/E0183.md"),
E0184: include_str!("./error_codes/E0184.md"),
E0185: include_str!("./error_codes/E0185.md"),
E0186: include_str!("./error_codes/E0186.md"),
Expand Down Expand Up @@ -513,7 +514,6 @@ E0785: include_str!("./error_codes/E0785.md"),
// E0173, // manual implementations of unboxed closure traits are experimental
// E0174,
// E0182, // merged into E0229
E0183,
// E0187, // cannot infer the kind of the closure
// E0188, // can not cast an immutable reference to a mutable pointer
// E0189, // deprecated: can only cast a boxed pointer to a boxed object
Expand Down
39 changes: 39 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0183.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Manual implemetation of a `Fn*` trait.

Erroneous code example:

```compile_fail,E0183
struct MyClosure {
foo: i32
}

impl FnOnce<()> for MyClosure { // error
type Output = ();
extern "rust-call" fn call_once(self, args: ()) -> Self::Output {
println!("{}", self.foo);
}
}
```

Manually implementing `Fn`, `FnMut` or `FnOnce` is unstable
and requires `#![feature(fn_traits, unboxed_closures)]`.

```
#![feature(fn_traits, unboxed_closures)]

struct MyClosure {
foo: i32
}

impl FnOnce<()> for MyClosure { // ok!
type Output = ();
extern "rust-call" fn call_once(self, args: ()) -> Self::Output {
println!("{}", self.foo);
}
}
```

The argumements must be a tuple representing the argument list.
For more info, see the [tracking issue][iss29625]:

[iss29625]: https://github.com/rust-lang/rust/issues/29625
72 changes: 29 additions & 43 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,10 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
}

fn collect_bang(
&mut self,
mac: ast::MacCall,
span: Span,
kind: AstFragmentKind,
) -> AstFragment {
fn collect_bang(&mut self, mac: ast::MacCall, kind: AstFragmentKind) -> AstFragment {
// cache the macro call span so that it can be
// easily adjusted for incremental compilation
let span = mac.span();
self.collect(kind, InvocationKind::Bang { mac, span })
}

Expand Down Expand Up @@ -1087,25 +1085,19 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
let MacCallStmt { mac, style, attrs, .. } = mac.into_inner();
Ok((style == MacStmtStyle::Semicolon, mac, attrs.into()))
}
StmtKind::Item(ref item) if matches!(item.kind, ItemKind::MacCall(..)) => {
match stmt.kind {
StmtKind::Item(item) => match item.into_inner() {
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
Ok((mac.args.need_semicolon(), mac, attrs))
}
_ => unreachable!(),
},
StmtKind::Item(item) if matches!(item.kind, ItemKind::MacCall(..)) => {
match item.into_inner() {
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
Ok((mac.args.need_semicolon(), mac, attrs))
}
_ => unreachable!(),
}
}
StmtKind::Semi(ref expr) if matches!(expr.kind, ast::ExprKind::MacCall(..)) => {
match stmt.kind {
StmtKind::Semi(expr) => match expr.into_inner() {
ast::Expr { kind: ast::ExprKind::MacCall(mac), attrs, .. } => {
Ok((mac.args.need_semicolon(), mac, attrs.into()))
}
_ => unreachable!(),
},
StmtKind::Semi(expr) if matches!(expr.kind, ast::ExprKind::MacCall(..)) => {
match expr.into_inner() {
ast::Expr { kind: ast::ExprKind::MacCall(mac), attrs, .. } => {
Ok((mac.args.need_semicolon(), mac, attrs.into()))
}
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -1222,7 +1214,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {

if let ast::ExprKind::MacCall(mac) = expr.kind {
self.check_attributes(&expr.attrs, &mac);
self.collect_bang(mac, expr.span, AstFragmentKind::Expr).make_expr().into_inner()
self.collect_bang(mac, AstFragmentKind::Expr).make_expr().into_inner()
} else {
assign_id!(self, &mut expr.id, || {
ensure_sufficient_stack(|| noop_visit_expr(&mut expr, self));
Expand Down Expand Up @@ -1318,7 +1310,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {

if let ast::ExprKind::MacCall(mac) = expr.kind {
self.check_attributes(&expr.attrs, &mac);
self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr)
self.collect_bang(mac, AstFragmentKind::OptExpr)
.make_opt_expr()
.map(|expr| expr.into_inner())
} else {
Expand All @@ -1339,9 +1331,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}

visit_clobber(pat, |mut pat| match mem::replace(&mut pat.kind, PatKind::Wild) {
PatKind::MacCall(mac) => {
self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat()
}
PatKind::MacCall(mac) => self.collect_bang(mac, AstFragmentKind::Pat).make_pat(),
_ => unreachable!(),
});
}
Expand All @@ -1360,12 +1350,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
.make_stmts();
}

let span = stmt.span;
match self.take_stmt_bang(stmt) {
Ok((add_semicolon, mac, attrs)) => {
self.check_attributes(&attrs, &mac);
let mut stmts =
self.collect_bang(mac, span, AstFragmentKind::Stmts).make_stmts();
let mut stmts = self.collect_bang(mac, AstFragmentKind::Stmts).make_stmts();

// If this is a macro invocation with a semicolon, then apply that
// semicolon to the final statement produced by expansion.
Expand Down Expand Up @@ -1433,7 +1421,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
item.attrs = attrs;
item.and_then(|item| match item.kind {
ItemKind::MacCall(mac) => {
self.collect_bang(mac, span, AstFragmentKind::Items).make_items()
self.collect_bang(mac, AstFragmentKind::Items).make_items()
}
_ => unreachable!(),
})
Expand Down Expand Up @@ -1542,9 +1530,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
ast::AssocItemKind::MacCall(ref mac) => {
self.check_attributes(&item.attrs, &mac);
item.and_then(|item| match item.kind {
ast::AssocItemKind::MacCall(mac) => self
.collect_bang(mac, item.span, AstFragmentKind::TraitItems)
.make_trait_items(),
ast::AssocItemKind::MacCall(mac) => {
self.collect_bang(mac, AstFragmentKind::TraitItems).make_trait_items()
}
_ => unreachable!(),
})
}
Expand All @@ -1567,9 +1555,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
ast::AssocItemKind::MacCall(ref mac) => {
self.check_attributes(&item.attrs, &mac);
item.and_then(|item| match item.kind {
ast::AssocItemKind::MacCall(mac) => self
.collect_bang(mac, item.span, AstFragmentKind::ImplItems)
.make_impl_items(),
ast::AssocItemKind::MacCall(mac) => {
self.collect_bang(mac, AstFragmentKind::ImplItems).make_impl_items()
}
_ => unreachable!(),
})
}
Expand All @@ -1586,9 +1574,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
};

visit_clobber(ty, |mut ty| match mem::replace(&mut ty.kind, ast::TyKind::Err) {
ast::TyKind::MacCall(mac) => {
self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty()
}
ast::TyKind::MacCall(mac) => self.collect_bang(mac, AstFragmentKind::Ty).make_ty(),
_ => unreachable!(),
});
}
Expand All @@ -1613,9 +1599,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
ast::ForeignItemKind::MacCall(ref mac) => {
self.check_attributes(&foreign_item.attrs, &mac);
foreign_item.and_then(|item| match item.kind {
ast::ForeignItemKind::MacCall(mac) => self
.collect_bang(mac, item.span, AstFragmentKind::ForeignItems)
.make_foreign_items(),
ast::ForeignItemKind::MacCall(mac) => {
self.collect_bang(mac, AstFragmentKind::ForeignItems).make_foreign_items()
}
_ => unreachable!(),
})
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ pub enum UndefinedBehaviorInfo<'tcx> {
target_size: u64,
data_size: u64,
},
/// A discriminant of an uninhabited enum variant is written.
UninhabitedEnumVariantWritten,
}

impl fmt::Display for UndefinedBehaviorInfo<'_> {
Expand Down Expand Up @@ -391,6 +393,9 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
"scalar size mismatch: expected {} bytes but got {} bytes instead",
target_size, data_size
),
UninhabitedEnumVariantWritten => {
write!(f, "writing discriminant of an uninhabited enum")
}
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,18 @@ impl SourceMap {
})
}

/// Extends the given `Span` while the next character matches the predicate
pub fn span_extend_while(
&self,
span: Span,
f: impl Fn(char) -> bool,
) -> Result<Span, SpanSnippetError> {
self.span_to_source(span, |s, _start, end| {
let n = s[end..].char_indices().find(|&(_, c)| !f(c)).map_or(s.len() - end, |(i, _)| i);
Ok(span.with_hi(span.hi() + BytePos(n as u32)))
})
}

/// Extends the given `Span` to just after the next occurrence of `c`.
pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
if let Ok(next_source) = self.span_to_next_source(sp) {
Expand Down Expand Up @@ -1013,6 +1025,32 @@ impl SourceMap {
let source_file = &self.files()[source_file_index];
source_file.is_imported()
}

/// Gets the span of a statement. If the statement is a macro expansion, the
/// span in the context of the block span is found. The trailing semicolon is included
/// on a best-effort basis.
pub fn stmt_span(&self, stmt_span: Span, block_span: Span) -> Span {
if !stmt_span.from_expansion() {
return stmt_span;
}
let mac_call = original_sp(stmt_span, block_span);
self.mac_call_stmt_semi_span(mac_call).map_or(mac_call, |s| mac_call.with_hi(s.hi()))
}

/// Tries to find the span of the semicolon of a macro call statement.
/// The input must be the *call site* span of a statement from macro expansion.
///
/// v output
/// mac!();
/// ^^^^^^ input
pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> {
let span = self.span_extend_while(mac_call, char::is_whitespace).ok()?;
let span = span.shrink_to_hi().with_hi(BytePos(span.hi().0.checked_add(1)?));
if self.span_to_snippet(span).as_deref() != Ok(";") {
return None;
}
Some(span)
}
}

#[derive(Clone)]
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_typeck/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ impl<'tcx> Bounds<'tcx> {
})
});

sized_predicate
.into_iter()
.chain(self.region_bounds.iter().map(|&(region_bound, span)| {
self.region_bounds
.iter()
.map(|&(region_bound, span)| {
(
region_bound
.map_bound(|region_bound| ty::OutlivesPredicate(param_ty, region_bound))
.to_predicate(tcx),
span,
)
}))
})
.chain(self.trait_bounds.iter().map(|&(bound_trait_ref, span, constness)| {
let predicate = bound_trait_ref.with_constness(constness).to_predicate(tcx);
(predicate, span)
Expand All @@ -83,6 +83,7 @@ impl<'tcx> Bounds<'tcx> {
.iter()
.map(|&(projection, span)| (projection.to_predicate(tcx), span)),
)
.chain(sized_predicate.into_iter())
.collect()
}
}
9 changes: 7 additions & 2 deletions compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,8 +1171,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
return None;
}
let original_span = original_sp(last_stmt.span, blk.span);
Some((original_span.with_lo(original_span.hi() - BytePos(1)), needs_box))
let span = if last_stmt.span.from_expansion() {
let mac_call = original_sp(last_stmt.span, blk.span);
self.tcx.sess.source_map().mac_call_stmt_semi_span(mac_call)?
} else {
last_stmt.span.with_lo(last_stmt.span.hi() - BytePos(1))
};
Some((span, needs_box))
}

// Instantiates the given path, which must refer to an item with the given
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::intrinsics;
/// ```
#[inline]
#[stable(feature = "unreachable", since = "1.27.0")]
#[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
pub const unsafe fn unreachable_unchecked() -> ! {
// SAFETY: the safety contract for `intrinsics::unreachable` must
// be upheld by the caller.
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ extern "rust-intrinsic" {
/// reach code marked with this function.
///
/// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
#[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
pub fn unreachable() -> !;

/// Informs the optimizer that a condition is always true.
Expand Down
Loading