@@ -113,15 +113,25 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a ty::ctxt<'tcx>,
113113 _ => None
114114 } ,
115115 Some ( ast_map:: NodeTraitItem ( ti) ) => match ti. node {
116- ast:: ConstTraitItem ( _, ref default ) => {
116+ ast:: ConstTraitItem ( _, _ ) => {
117117 match maybe_ref_id {
118+ // If we have a trait item, and we know the expression
119+ // that's the source of the obligation to resolve it,
120+ // `resolve_trait_associated_const` will select an impl
121+ // or the default.
118122 Some ( ref_id) => {
119123 let trait_id = ty:: trait_of_item ( tcx, def_id)
120124 . unwrap ( ) ;
121125 resolve_trait_associated_const ( tcx, ti, trait_id,
122126 ref_id)
123127 }
124- None => default. as_ref ( ) . map ( |expr| & * * expr) ,
128+ // Technically, without knowing anything about the
129+ // expression that generates the obligation, we could
130+ // still return the default if there is one. However,
131+ // it's safer to return `None` than to return some value
132+ // that may differ from what you would get from
133+ // correctly selecting an impl.
134+ None => None
125135 }
126136 }
127137 _ => None
@@ -149,17 +159,19 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a ty::ctxt<'tcx>,
149159 ast:: ItemConst ( _, ref const_expr) => Some ( const_expr. id ) ,
150160 _ => None
151161 } ,
152- csearch:: FoundAst :: Found ( & ast:: IITraitItem ( _ , ref ti) ) => match ti. node {
153- ast:: ConstTraitItem ( _, ref default ) => {
162+ csearch:: FoundAst :: Found ( & ast:: IITraitItem ( trait_id , ref ti) ) => match ti. node {
163+ ast:: ConstTraitItem ( _, _ ) => {
154164 used_ref_id = true ;
155165 match maybe_ref_id {
166+ // As mentioned in the comments above for in-crate
167+ // constants, we only try to find the expression for
168+ // a trait-associated const if the caller gives us
169+ // the expression that refers to it.
156170 Some ( ref_id) => {
157- let trait_id = ty:: trait_of_item ( tcx, def_id)
158- . unwrap ( ) ;
159171 resolve_trait_associated_const ( tcx, ti, trait_id,
160172 ref_id) . map ( |e| e. id )
161173 }
162- None => default . as_ref ( ) . map ( |expr| expr . id ) ,
174+ None => None
163175 }
164176 }
165177 _ => None
@@ -173,7 +185,7 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a ty::ctxt<'tcx>,
173185 // If we used the reference expression, particularly to choose an impl
174186 // of a trait-associated const, don't cache that, because the next
175187 // lookup with the same def_id may yield a different result.
176- if used_ref_id {
188+ if ! used_ref_id {
177189 tcx. extern_const_statics
178190 . borrow_mut ( ) . insert ( def_id,
179191 expr_id. unwrap_or ( ast:: DUMMY_NODE_ID ) ) ;
0 commit comments