Implement use
associated items of traits
This commit is contained in:
parent
4e5fec2f1e
commit
5079acc060
21 changed files with 374 additions and 42 deletions
|
@ -1183,7 +1183,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
|
|||
let in_module_is_extern = !in_module.def_id().is_local();
|
||||
in_module.for_each_child(self, |this, ident, ns, name_binding| {
|
||||
// avoid non-importable candidates
|
||||
if !name_binding.is_importable() {
|
||||
if !name_binding.is_importable()
|
||||
// FIXME(import_trait_associated_functions): remove this when `import_trait_associated_functions` is stable
|
||||
|| name_binding.is_assoc_const_or_fn()
|
||||
&& !this.tcx.features().import_trait_associated_functions()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,9 +17,10 @@ use rustc_session::lint::builtin::{
|
|||
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
|
||||
REDUNDANT_IMPORTS, UNUSED_IMPORTS,
|
||||
};
|
||||
use rustc_session::parse::feature_err;
|
||||
use rustc_span::edit_distance::find_best_match_for_name;
|
||||
use rustc_span::hygiene::LocalExpnId;
|
||||
use rustc_span::{Ident, Span, Symbol, kw};
|
||||
use rustc_span::{Ident, Span, Symbol, kw, sym};
|
||||
use smallvec::SmallVec;
|
||||
use tracing::debug;
|
||||
|
||||
|
@ -829,6 +830,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
|
|||
// Don't update the resolution, because it was never added.
|
||||
Err(Determined) if target.name == kw::Underscore => {}
|
||||
Ok(binding) if binding.is_importable() => {
|
||||
if binding.is_assoc_const_or_fn()
|
||||
&& !this.tcx.features().import_trait_associated_functions()
|
||||
{
|
||||
feature_err(
|
||||
this.tcx.sess,
|
||||
sym::import_trait_associated_functions,
|
||||
import.span,
|
||||
"`use` associated items of traits is unstable",
|
||||
)
|
||||
.emit();
|
||||
}
|
||||
let imported_binding = this.import(binding, import);
|
||||
target_bindings[ns].set(Some(imported_binding));
|
||||
this.define(parent, target, ns, imported_binding);
|
||||
|
|
|
@ -920,10 +920,13 @@ impl<'ra> NameBindingData<'ra> {
|
|||
}
|
||||
|
||||
fn is_importable(&self) -> bool {
|
||||
!matches!(
|
||||
self.res(),
|
||||
Res::Def(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy, _)
|
||||
)
|
||||
!matches!(self.res(), Res::Def(DefKind::AssocTy, _))
|
||||
}
|
||||
|
||||
// FIXME(import_trait_associated_functions): associate `const` or `fn` are not importable unless
|
||||
// the feature `import_trait_associated_functions` is enable
|
||||
fn is_assoc_const_or_fn(&self) -> bool {
|
||||
matches!(self.res(), Res::Def(DefKind::AssocConst | DefKind::AssocFn, _))
|
||||
}
|
||||
|
||||
fn macro_kind(&self) -> Option<MacroKind> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue