Skip to content

Commit

Permalink
hir: introduce lang items for AST lowering
Browse files Browse the repository at this point in the history
This commit adds new lang items which will be used in AST lowering once
`QPath::LangItem` is introduced.

Co-authored-by: Matthew Jasper <mjjasper1@gmail.com>
Signed-off-by: David Wood <david@davidtw.co>
  • Loading branch information
davidtwco and matthewjasper committed Aug 16, 2020
1 parent 734441c commit 7dee5f8
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions library/core/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ pub trait Into<T>: Sized {
))]
pub trait From<T>: Sized {
/// Performs the conversion.
#[cfg_attr(not(bootstrap), lang = "from")]
#[stable(feature = "rust1", since = "1.0.0")]
fn from(_: T) -> Self;
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/future/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub trait Future {
/// [`Context`]: ../task/struct.Context.html
/// [`Waker`]: ../task/struct.Waker.html
/// [`Waker::wake`]: ../task/struct.Waker.html#method.wake
#[cfg_attr(not(bootstrap), lang = "poll")]
#[stable(feature = "futures_api", since = "1.36.0")]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
}
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ unsafe impl Sync for ResumeTy {}
/// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
/// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
// This is `const` to avoid extra errors after we recover from `const async fn`
#[cfg_attr(not(bootstrap), lang = "from_generator")]
#[doc(hidden)]
#[unstable(feature = "gen_future", issue = "50547")]
#[inline]
Expand Down Expand Up @@ -85,6 +86,7 @@ where
GenFuture(gen)
}

#[cfg_attr(not(bootstrap), lang = "get_context")]
#[doc(hidden)]
#[unstable(feature = "gen_future", issue = "50547")]
#[inline]
Expand Down
1 change: 1 addition & 0 deletions library/core/src/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pub trait IntoIterator {
/// assert_eq!(Some(3), iter.next());
/// assert_eq!(None, iter.next());
/// ```
#[cfg_attr(not(bootstrap), lang = "into_iter")]
#[stable(feature = "rust1", since = "1.0.0")]
fn into_iter(self) -> Self::IntoIter;
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub trait Iterator {
/// assert_eq!(None, iter.next());
/// assert_eq!(None, iter.next());
/// ```
#[cfg_attr(not(bootstrap), lang = "next")]
#[stable(feature = "rust1", since = "1.0.0")]
fn next(&mut self) -> Option<Self::Item>;

Expand Down
7 changes: 7 additions & 0 deletions library/core/src/ops/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::hash::Hash;
/// [`IntoIterator`]: ../iter/trait.Iterator.html
/// [`Iterator`]: ../iter/trait.IntoIterator.html
/// [slicing index]: ../slice/trait.SliceIndex.html
#[cfg_attr(not(bootstrap), lang = "RangeFull")]
#[doc(alias = "..")]
#[derive(Copy, Clone, Default, PartialEq, Eq, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -70,6 +71,7 @@ impl fmt::Debug for RangeFull {
/// assert_eq!(arr[1.. 3], [ 1,2 ]); // Range
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
/// ```
#[cfg_attr(not(bootstrap), lang = "Range")]
#[doc(alias = "..")]
#[derive(Clone, Default, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -178,6 +180,7 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// ```
///
/// [`Iterator`]: ../iter/trait.IntoIterator.html
#[cfg_attr(not(bootstrap), lang = "RangeFrom")]
#[doc(alias = "..")]
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -260,6 +263,7 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
/// [`IntoIterator`]: ../iter/trait.Iterator.html
/// [`Iterator`]: ../iter/trait.IntoIterator.html
/// [slicing index]: ../slice/trait.SliceIndex.html
#[cfg_attr(not(bootstrap), lang = "RangeTo")]
#[doc(alias = "..")]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -328,6 +332,7 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
/// assert_eq!(arr[1..=3], [ 1,2,3 ]); // RangeInclusive
/// ```
#[cfg_attr(not(bootstrap), lang = "RangeInclusive")]
#[doc(alias = "..=")]
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[stable(feature = "inclusive_range", since = "1.26.0")]
Expand Down Expand Up @@ -359,6 +364,7 @@ impl<Idx> RangeInclusive<Idx> {
///
/// assert_eq!(3..=5, RangeInclusive::new(3, 5));
/// ```
#[cfg_attr(not(bootstrap), lang = "range_inclusive_new")]
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
#[inline]
#[rustc_promotable]
Expand Down Expand Up @@ -555,6 +561,7 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// [`IntoIterator`]: ../iter/trait.Iterator.html
/// [`Iterator`]: ../iter/trait.IntoIterator.html
/// [slicing index]: ../slice/trait.SliceIndex.html
#[cfg_attr(not(bootstrap), lang = "RangeToInclusive")]
#[doc(alias = "..=")]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[stable(feature = "inclusive_range", since = "1.26.0")]
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/ops/try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ pub trait Try {
/// in the return type of the enclosing scope (which must itself implement
/// `Try`). Specifically, the value `X::from_error(From::from(e))`
/// is returned, where `X` is the return type of the enclosing function.
#[cfg_attr(not(bootstrap), lang = "into_result")]
#[unstable(feature = "try_trait", issue = "42327")]
fn into_result(self) -> Result<Self::Ok, Self::Error>;

/// Wrap an error value to construct the composite result. For example,
/// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
#[cfg_attr(not(bootstrap), lang = "from_error")]
#[unstable(feature = "try_trait", issue = "42327")]
fn from_error(v: Self::Error) -> Self;

/// Wrap an OK value to construct the composite result. For example,
/// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.
#[cfg_attr(not(bootstrap), lang = "from_ok")]
#[unstable(feature = "try_trait", issue = "42327")]
fn from_ok(v: Self::Ok) -> Self;
}
2 changes: 2 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ use crate::{
#[stable(feature = "rust1", since = "1.0.0")]
pub enum Option<T> {
/// No value
#[cfg_attr(not(bootstrap), lang = "None")]
#[stable(feature = "rust1", since = "1.0.0")]
None,
/// Some value `T`
#[cfg_attr(not(bootstrap), lang = "Some")]
#[stable(feature = "rust1", since = "1.0.0")]
Some(#[stable(feature = "rust1", since = "1.0.0")] T),
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ impl<P: Deref> Pin<P> {
/// ```
///
/// [`mem::swap`]: ../../std/mem/fn.swap.html
#[cfg_attr(not(bootstrap), lang = "new_unchecked")]
#[stable(feature = "pin", since = "1.33.0")]
#[inline(always)]
pub unsafe fn new_unchecked(pointer: P) -> Pin<P> {
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,12 @@ use crate::{convert, fmt};
#[stable(feature = "rust1", since = "1.0.0")]
pub enum Result<T, E> {
/// Contains the success value
#[cfg_attr(not(bootstrap), lang = "Ok")]
#[stable(feature = "rust1", since = "1.0.0")]
Ok(#[stable(feature = "rust1", since = "1.0.0")] T),

/// Contains the error value
#[cfg_attr(not(bootstrap), lang = "Err")]
#[stable(feature = "rust1", since = "1.0.0")]
Err(#[stable(feature = "rust1", since = "1.0.0")] E),
}
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/task/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::result::Result;
#[stable(feature = "futures_api", since = "1.36.0")]
pub enum Poll<T> {
/// Represents that a value is immediately ready.
#[cfg_attr(not(bootstrap), lang = "Ready")]
#[stable(feature = "futures_api", since = "1.36.0")]
Ready(#[stable(feature = "futures_api", since = "1.36.0")] T),

Expand All @@ -18,6 +19,7 @@ pub enum Poll<T> {
/// When a function returns `Pending`, the function *must* also
/// ensure that the current task is scheduled to be awoken when
/// progress can be made.
#[cfg_attr(not(bootstrap), lang = "Pending")]
#[stable(feature = "futures_api", since = "1.36.0")]
Pending,
}
Expand Down
36 changes: 35 additions & 1 deletion src/librustc_hir/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
pub use self::LangItem::*;

use crate::def_id::DefId;
use crate::Target;
use crate::{MethodKind, Target};

use rustc_ast::ast;
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -307,4 +307,38 @@ language_item_table! {
CountCodeRegionFnLangItem, sym::count_code_region, count_code_region_fn, Target::Fn;
CoverageCounterAddFnLangItem, sym::coverage_counter_add, coverage_counter_add_fn, Target::Fn;
CoverageCounterSubtractFnLangItem, sym::coverage_counter_subtract, coverage_counter_subtract_fn, Target::Fn;

// Language items from AST lowering
TryFromError, sym::from_error, from_error_fn, Target::Method(MethodKind::Trait { body: false });
TryFromOk, sym::from_ok, from_ok_fn, Target::Method(MethodKind::Trait { body: false });
TryIntoResult, sym::into_result, into_result_fn, Target::Method(MethodKind::Trait { body: false });

PollReady, sym::Ready, poll_ready_variant, Target::Variant;
PollPending, sym::Pending, poll_pending_variant, Target::Variant;

FromGenerator, sym::from_generator, from_generator_fn, Target::Fn;
GetContext, sym::get_context, get_context_fn, Target::Fn;

FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false });

FromFrom, sym::from, from_fn, Target::Method(MethodKind::Trait { body: false });

OptionSome, sym::Some, option_some_variant, Target::Variant;
OptionNone, sym::None, option_none_variant, Target::Variant;

ResultOk, sym::Ok, result_ok_variant, Target::Variant;
ResultErr, sym::Err, result_err_variant, Target::Variant;

IntoIterIntoIter, sym::into_iter, into_iter_fn, Target::Method(MethodKind::Trait { body: false });
IteratorNext, sym::next, next_fn, Target::Method(MethodKind::Trait { body: false});

PinNewUnchecked, sym::new_unchecked, new_unchecked_fn, Target::Method(MethodKind::Inherent);

RangeFrom, sym::RangeFrom, range_from_struct, Target::Struct;
RangeFull, sym::RangeFull, range_full_struct, Target::Struct;
RangeInclusiveStruct, sym::RangeInclusive, range_inclusive_struct, Target::Struct;
RangeInclusiveNew, sym::range_inclusive_new, range_inclusive_new_method, Target::Method(MethodKind::Inherent);
Range, sym::Range, range_struct, Target::Struct;
RangeToInclusive, sym::RangeToInclusive, range_to_inclusive_struct, Target::Struct;
RangeTo, sym::RangeTo, range_to_struct, Target::Struct;
}
2 changes: 2 additions & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ symbols! {
never_type,
never_type_fallback,
new,
new_unchecked,
next,
nll,
no,
Expand Down Expand Up @@ -828,6 +829,7 @@ symbols! {
quad_precision_float,
question_mark,
quote,
range_inclusive_new,
raw_dylib,
raw_identifiers,
raw_ref_op,
Expand Down

0 comments on commit 7dee5f8

Please sign in to comment.